For the life of me, I can't understand continuations. I think the problem stems from the fact that I don't understand is what they are for. All the examples that I've found in books or online are very trivial. They make me wonder, why anyone would even want continuations?
Here's a typical impractical example, from TSPL, which I believe is quite recognized book on the subject. In english, they describe the continuation as "what to do" with the result of a computation. OK, that's sort of understandable.
Then, the second example given:
(call/cc (lambda (k) (* 5 (k 4)))) => 4
How does this make any sense?? k
isn't even defined! How can this code be evaluated, when (k 4)
can't even be computed? Not to mention, how does call/cc
know to rip out the argument 4
to the inner most expression and return it? What happens to (* 5 ..
?? If this outermost expression is discarded, why even write it?
Then, a "less" trivial example stated is how to use call/cc
to provide a nonlocal exit from a recursion. That sounds like flow control directive, ie like break/return
in an imperative language, and not a computation.
And what is the purpose of going through these motions? If somebody needs the result of computation, why not just store it and recall later, as needed.
An expression's continuation is "the computation that will receive the result of that expression". For example, in the expression (+ 4 (+ 1 2)) the result of (+ 1 2) will be added to 4. The addition to 4 is that expression's continuation.
Taking a function f as its only argument, (call/cc f) within an expression is applied to the current continuation of the expression. For example ((call/cc f) e2) is equivalent to applying f to the current continuation of the expression.
Once the behavior of continuations ◦ has been explained, the second part shows how to use macros to build continuations in Common Lisp programs. Chapters 21–24 will all make use of the macros defined here. One of the principal ways in which Scheme differs from Common Lisp is its explicit support for continuations.
Forget about call/cc
for a moment. Every expression/statement, in any programming language, has a continuation - which is, what you do with the result. In C, for example,
x = (1 + (2 * 3)); printf ("Done");
has the continuation of the math assignment being printf(...)
; the continuation of (2 * 3)
is 'add 1; assign to x; printf(...)'. Conceptually the continuation is there whether or not you have access to it. Think for a moment what information you need for the continuation - the information is 1) the heap memory state (in general), 2) the stack, 3) any registers and 4) the program counter.
So continuations exist but usually they are only implicit and can't be accessed.
In Scheme, and a few other languages, you have access to the continuation. Essentially, behind your back, the compiler+runtime bundles up all the information needed for a continuation, stores it (generally in the heap) and gives you a handle to it. The handle you get is the function 'k' - if you call that function you will continue exactly after the call/cc
point. Importantly, you can call that function multiple times and you will always continue after the call/cc
point.
Let's look at some examples:
> (+ 2 (call/cc (lambda (cont) 3))) 5
In the above, the result of call/cc
is the result of the lambda
which is 3. The continuation wasn't invoked.
Now let's invoke the continuation:
> (+ 2 (call/cc (lambda (cont) (cont 10) 3))) 12
By invoking the continuation we skip anything after the invocation and continue right at the call/cc
point. With (cont 10)
the continuation returns 10
which is added to 2 for 12.
Now let's save the continuation.
> (define add-2 #f) > (+ 2 (call/cc (lambda (cont) (set! add-2 cont) 3))) 5 > (add-2 10) 12 > (add-2 100) 102
By saving the continuation we can use it as we please to 'jump back to' whatever computation followed the call/cc
point.
Often continuations are used for a non-local exit. Think of a function that is going to return a list unless there is some problem at which point '()
will be returned.
(define (hairy-list-function list) (call/cc (lambda (cont) ;; process the list ... (when (a-problem-arises? ...) (cont '())) ;; continue processing the list ... value-to-return)))
Here is text from my class notes: http://tmp.barzilay.org/cont.txt. It is based on a number of sources, and is much extended. It has motivations, basic explanations, more advanced explanations for how it's done, and a good number of examples that go from simple to advanced, and even some quick discussion of delimited continuations.
(I tried to play with putting the whole text here, but as I expected, 120k of text is not something that makes SO happy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With