Search code examples
schemecontinuations

Continuation in Scheme


I think I got what a continuations is (in general), but I can't understand how it is used in Scheme.

Consider this example (from wikipedia call/cc)

(define (f return)
  (return 2)
  3)
(display (call/cc f)) ;=> 2

I cannot understand why:

  • the continuation is implicit?right?

  • How is the continuation in this case?


Solution

  • The continuation is the "rest of the computation" that remains to be executed. In your particular example, you could think of this as being (display []) where [] is a hole to be plugged with a value. That is, at the point that call/cc is invoked, what remains to be done is the call to display.

    What call/cc does is take this continuation and puts it in a special value that can be applied like a function. It passes this value to its argument (here f). In f, the continuation is bound to return. So (return 2) will basically plug 2 into the continuation, i.e., (display 2).

    I don't think this example is actually very helpful, so I think you should read PLAI if you're interested in learning more about continuations (see Part VII). Another good source is these lecture notes by Dan Friedman.