javascriptcallcc

call/CC with closures


Wikipedia mentions that "In any language which supports closures and proper tail calls, it is possible to write programs in continuation-passing style and manually implement call/cc."

How would one implement this function in for example in javascript? I know that javascript doesn't do tco, but assuming that stack space doesn't run out


Solution

  • It is not possible to write a call/cc implementation in JavaScript:

    JavaScript does not meet the requirement of "proper tail calls" (no additional stack creation). However, I believe that a form of continuations, such as that found in Jetty, using exceptions is possible. "CPS" is as easy as passing a function-object, although ultimately it runs into stack issues as well unless backed out occasionally.

    Happy coding.