Next: Multiple Values, Previous: while do, Up: Control Mechanisms
The ability to explicitly capture continuations using
call-with-current-continuation (also often called call/cc
for short), and to invoke such continuations later any number of times,
and from any other point in a program, provides maybe the most powerful
control structure known. All other control structures, such as loops
and coroutines, can be emulated using continuations.
The implementation of continuations in Guile is not as efficient as one
might hope, because it is constrained by the fact that Guile is designed
to cooperate with programs written in other languages, such as C, which
do not know about continuations. So continuations should be used when
there is no other simple way of achieving the desired behaviour, or
where the advantages of the elegant continuation mechanism outweigh the
need for optimum performance. If you find yourself using call/cc
for escape procedures and your program is running too slow, you might
want to use exceptions (see Exceptions) instead.
Capture the current continuation and call proc with the captured continuation as the single argument. This continuation can then be called with arbitrarily many arguments. Such a call will work like a goto to the invocation location of
call-with-current-continuation, passing the arguments in a way that they are returned by the call tocall-with-current-continuation. Since it is legal to store the captured continuation in a variable or to pass it to other procedures, it is possible that a procedure returns more than once, even if it is called only one time. This can be confusing at times.
Return
#tiff obj is a continuation.
(define kont #f)
(call-with-current-continuation
(lambda (k)
(set! kont k)
1))
⇒ 1
(kont 2)
⇒ 2
(continuation? kont)
⇒ #t