|
Home
>
2. program control
>
2.2 control flow
>
2.2.1 continuations
|
Previous
Next
|
|
|
|
|
|
Thesaurus
|
continuation
noun
- Uninterrupted existence or succession
- A continuing after interruption
|
|
Peter Norvig
|
Sometime it is tricky to ensure that code gets done at the right
time, and end up in the right place.The answer is to pass in a
fuction that will tell us what code to generate later. Such a
function is often called a continuation, because it tells us where to
continue computing.
|
|
Shriram
Krishnamurthi
|
Capturing Continuations
In Scheme, we create a value representing the continuation using
one of two related constructs. The traditional form is called
call/cc, short for “call with current continuation”. call/cc consumes
a procedure as an argument, and invokes this procedure with a
continuation. That is, uses of call/cc typically look like
(call/cc
(lambda (k)
··· k ···)) ;; k is the continuation
Because the extra lambda is extremely tiresome, however,
Scheme provides a nicer interface to capturing the current
continuation: you may instead equivalently write
(let/cc k
··· k ···)) ;; k is bound to the continuation
Note that let/cc is a binding construct: it introduces a new scope,
binding the named identifier (k, above) in that context. For the rest
of this material, we’ll use let/cc rather than call/cc.
|
|
|
|
|
|
|
|
|