Question: I have 2 vectors A, B. Using `A.angleTo(B)`, I know the mangitude of the angle between them. 

How do I find the sign of this angle? 
Maybe we need to use some reference vector for this?

Answer: 
First off. All languages has continuations. When you do `7 + n * 5` JavaScript reorders that to `mul_k(n, 5, (_v) => (add _v 7 k)` where `k` is a function that does everything that happnes after that expression. 

Now the first argument to `mul_k` is a continuation. Nothing scary about it except for one little think. You never actually return anything any longer. Every "return" is just passed to it's continuation which always is a tail call. 

A recursive function that itself isn't tail recursive will produce a new closure at each step and nest the next. These are are stored on the heap so non tail recursive functions becomes tail calls with lots of nested closures created.

Here is a small example:

```scm
(define (get-c) (call/cc (lambda (cont) cont)))

(let ((example (get-c)))
  (displayln example)
  (if (procedure? example)
      (example 10)
      "done"))

```

Lets imagine this is the whole program. Lets just write this to JavaScript. 

```js
// simple CPS version of displayln
const displayln = (arg, k) k(console.log(arg));

// simple CPS version of procedure?
const isProcedure = (arg, k) k(arg instanceOf Function);

// Simple CPS version of call/cc
const callCC = (userFun, callCCK) 
  => userFun((result, actualK) => callCCK(result), callCCK);

// simple top level continutation. Not a CPS function.
const kTopLevel = console.log;

// the user function get-c transformed into CPS
const getC = getCK => callCC((cont, k) => k(cont), getCK);

// the let code transformed into CPS
getC((example) => // c1
  displayln(example, (_undefined) => // c2 
    isProcedure(example, (result) => // c3
      result ? example(10, kTopLevel) : kTopLevel("done"))

```

Here is what's happening:

* `getC` gets the whole continuation (`c1`) and it calls `callCC`
* `callCC` calls `c1` with gets `(result, _) => c1(result)` as `example`
* `display` prints `example`, the continuation, passes its undefinded return value to it's continuation `c2`
* `isPorcedure` on `example` and it being a continuation it passes `true` as `result` to continuation `c3`
* being `true` it calls `example(10, kTopLevel)` which becomes `c1(10)`, thus `10` as example
* `display` prints èxample`, the number`10`, passes its underfined return value to its continuation`c2`
* isProcedure on `example` passes `false` as `result` to continuation `c3`
* Being `false` it calls `TopLevel("dome")`
* TopLevel prints "done"

As you can see. `call/cc` is easy peasy as long as the code is transformed to CPS *before* execution. JavaScript supports CPS very well. 

To help with how to convert code to CPS Matt Might has done it [countless times](http://matt.might.net/articles/) in his essays. Look for continuations in that list. He has even done it [in JavaScript](http://matt.might.net/articles/by-example-continuation-passing-style/). 

Now if your implementation needs to beinterpreted you could do the same conversion in Scheme and interpret that instead of user code. If you have continuation barriers you just need CPS from `call/cc` until the barrier.
**synchronous call/cc**

`callcc` can be implemented in direct style using `try-catch`. The important bit is that the continuation must "box" the return value and "unbox" it in `catch` to avoid swallowing real errors -

```js
const callcc = f => {
  class Box { constructor(v) { this.unbox = v } }
  try { return f(value => { throw new Box(value) }) }
  catch (e) { if (e instanceof Box) return e.unbox; throw e  }
}

console.log(5 + callcc(exit => 10 * 3))
console.log(5 + callcc(exit => 10 * exit(3)))
console.log(5 + callcc(exit => { exit(10); return 3 }))
console.log(5 + callcc(exit => { exit(10); throw Error("test failed") }))

try {
  console.log(5 + callcc(exit => { throw Error("test passed!") }))
}
catch (e) {
  console.error(e)
}
```

```css
.as-console-wrapper { min-height: 100%; top: 0; }
```

```none
35                     ✅ 5 + 10 * 3
8                      ✅ 5 + 3
15                     ✅ 5 + 10
15                     ✅ 5 + 10
Error: test passed!    ✅ Errors are not swallowed

```

`callcc` was originally implemented in [this Q&A](https://stackoverflow.com/a/72024487/633183). Read on for additional info and examples.