============[ Continuations, AMB example ]============ See [UPDATE:]-ed parts throughout scheme/continuations.txt Re-read it even if you read it before; perhaps some things will come into focus. ============[ Continuation-Passing Style ]============ The CPS is another important application of the continuations idea---except in CPS you write all continuations explicitly, and pass them explicitly. Under this style of programming, no function ever returns; instead, it calls the one-argument function that is passed into it, and represents the continuation, "all the work from here on". That function also never returns, etc. Think of the program as being turned "inside-out". Normally, you write the program from the first expressions you want executed to the last; in LISP and Scheme, the s-expressions evaluated later contain those executed earlier. In CPS, it is as though you need to start from the very last things you want done, and pass them as a single-argument function to the things you want done just before, and so on, to the very first things, which get the much-grown continuation as an argument. For simple examples of rewriting functions into CPS, see scheme/cps-simple-examples. =================[ CPS in LISP, CPS rewriting ]=============== Read OnLisp's section 20.2 to see how LISP functions can be rewritten to use continuations *and* CPS with some clever macros. These macros add the continuation argument to every function's definition, and annotate every function's returned values with the extra call to this argument (this needs to be done on each path through which a function may return!). With reasonable restrictions on how the functions are written, the transformation to CPS is accomplished by these macros. ============[ CPS in Javascript, web programming ]============ See another of Matt Might's blog posts (skip the factorial example): http://matt.might.net/articles/by-example-continuation-passing-style/ about the use of CPS for non-blocking programming---in Javascript. [UPDATE:] See also useful discussion here: http://stackoverflow.com/questions/1888702/are-there-problems-that-cannot-be-written-using-tail-recursion