[plt-scheme] Understanding continuations

From: Grant Rettke (grettke at acm.org)
Date: Sat Jul 19 01:44:58 EDT 2008

I'm trying to visualize and understand continuations (this part of the
work in another thread [Confusing continuation behavior (a question
about)]).

If I write:

#lang scheme
(define cont #f)
(define (foo)
  (call/cc
   (lambda (arg-cont)
     (set! cont arg-cont)))
  13)
(+ 1 2 3 (foo))

Could I visualize the continuation as?

(lambda (argument)
  (+ 1 2 3 13))

If I write:

#lang scheme
(define cont #f)
(define (foo)
  (call/cc
   (lambda (arg-cont)
     (set! cont arg-cont)
     13)))
(+ 1 2 3 (foo))

Could I visualize the continuation as?

(lambda (argument)
  (+ 1 2 3 argument))

The difference between the two is this:

1. In the first one, the saved continuation 'cont' will always have
the same value; that of the current continuation with the value of 13
passed in to it. Every time that continuation is applied the result
will be the same. It is like memoization.

2. In the second one, the continuation is awaiting a value of the
function foo (or it is waiting for the expression of the call/cc
block). So at any other point, that continuation could be passed
"something else".

In the first one, "what happens next" could never change, because it
has been determined. In the second one, "what happens next" could
change, because the continuation is still waiting for a value.

Is that right?


Posted on the users mailing list.