call/cc stands for call-with-current-continuation
. In addition to Scheme
also has call/cc.
``What is call/cc?'', you ask. Well, first, consider the notion of ``what the program is going to do next''. This is the basis behind a continuation. In a function, the continuation is the context to
which the function will return. There is an intermediate representation, often used in compiling functional langauges, called `continuation-passing style' that basically makes every function call into a tail call, possibly along with an additional argument indicating where the tail call will go. But I digress.
In scheme, call/cc is a function that takes as its sole argument
a second function of a single argument (call it `foo'). It calls
foo with an argument (call it `bar') which is itself a function
of a single argument (confused yet? it gets better). foo can proceed as though it had been called normally. When it calls bar (say with argument `baz'), however, control returns to the original
caller of call/cc, and it is as though call/cc returned with the value
As described above, this would be useful for implementing break,
throw/catch, etc., but without any of that annoying clarity. Here's the cool part: the continuation created (that's `bar' above) has indefinite extent. That means you can store it in a variable somewhere and call it much later, possibly after foo has returned normally, or has called already called bar N times. It is useful for,
among other things, implementing coroutines.
I believe it was Guy L. Steele who proved that any traditional control structure could be implemented with call/cc, but I may be mistaking him for someone else.
For more information on continuations, see Compiling with Continuations by Andrew Appel. For more information on
call/cc as it exists in Scheme, see R5RS.