The merits of call-with-current-continuation
The basic idea is that every expression in a Scheme program has a
context to which its return value is fed once it's been computed.
This amounts to the "future" of that expression, and the underlying
implementation usually implements it as a stack of activation records.
Most programming languages support capturing the context via some
catch/throw or exception handling or
setjmp/longjmp mechanism which allows you to restore
a context which is part of the current context, i.e., to unwind
part of the stack. This means that they work only "upwards" on
the stack. However, to implement sophisticated control
structures, you need to be able to:
- Restore the context even if it's not lying around on the stack.
- Restore it multiple times.
The most popular example are threads. From a C programmer's
viewpoint, all threads share the same global memory, but each has
its own stack aka context.
CALL-WITH-CURRENT-CONTINUATION simply converts its
own context (the "current continuation") into a one-argument
procedure. When you call that procedure, the current context is
replaced by that of the call to
CALL-WITH-CURRENT-CONTINUATION.
In connection with one global reference (which Scheme gives you
easily), you can model any control structure (in a
certain formal sense). CL's mechanism is not strong enough for
this.
(Another good example is the Icon language which uses a control
structure called "generator" as the backbone of a very powerful model
for text processing---much more powerful than Emacs Lisp. Generators
are a slightly generalized version of coroutines, and trivial to
implement in Scheme.)
Good explanations of continuations are in:
@BOOK{FriedmanWandHaynes1992,
author = "Daniel P. Friedman and Mitchell Wand and Christopher
T. Haynes",
title = "Essentials of Programming Languages",
publisher = "MIT Press and McGraw-Hill",
year = 1992,
annote = "EOPL",
ISBN = {MIT 0-262-06145-7, McGraw-Hill 0-07-022443-9}
}
@BOOK{SpringerFriedman1989,
author = {George Springer
and Daniel P. Friedman},
title = {Scheme and the Art of Programming},
year = 1989,
publisher = {MIT Press and McGraw-Hill},
keywords = {scheme-art}
}
@Book{Queinnec1994,
author = {Christian Queinnec},
title = {Lisp in Small Pieces},
publisher = {Cambridge University Press},
year = 1994
}
Michael Sperber [Mr. Preprocessor]
Last modified: Tue Jul 14 16:04:56 MST 1998