Scheme Continuations

[Caution]Caution
The material in these notes may or may not be presented, but as it is rather interesting stuff, so I've made the notes available for those interested.

Here is an example of a Scheme program that makes use of a continuation, k

define (f k)
 (k 1)
 (k 2)
 (+ 1 2))

(= 1 (call-with-current-continuation f))

This evaluates #t. The expressions (k 2) and (+ 1 2) are not evaluated.

Continuations can be used for all sorts of things but typically are only explictly dealt with in a few places in a Scheme program where their use dramatically simplifies code structure or improves performance. Furthermore, a good foundation in understanding the concepts behind continuations will make other programming constructs, e.g., exceptions, seem simple. Unfortunately, because continuations are such a comparatively unusual concept, good mainstream introductions to the concept and its application are hard to find. You may need to do a lot of reading and experimenting before the concept becomes clear for you. A good introduction to continuations can be found in The Scheme Programming Language, Second Edition by Kent Dybvig, which is also available online at http://www.scheme.com/tspl2ed.html. Another good introduction can be found at http://www.eleves.ens.fr/home/madore/computers/callcc.html but beware of broken links.

If you haven't found a good reference, yet, here is a quick definition from the Free On-Line Dictionary of Computing http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?Call-with-current-continuation which defines “call-with-current-continuation” as follows:

(call/cc) A Lisp control function that takes a function f as its argument and calls f, passing it the current continuation, which is itself a function, k. k, which represents the context of the call to call/cc, takes the result of call/cc (which is the result of f) and returns the final result of the whole program. Thus if, for example, the final result is to print the value returned by call/cc then anything passed to k will also be printed.