[plt-scheme] continuation question

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Mar 28 11:10:40 EDT 2008

At Fri, 28 Mar 2008 10:52:58 -0400, Robert Nikander wrote:
> I'm trying to better understand continuations.  When this code runs  
> at the top level, it does not loop, unless I use the the let instead  
> of the begin.  Why?  Is the call to "k" failing? If so, how come it  
> doesn't throw an error?
> (define k #f)
> ;(let ()
> (begin
>    (let/cc c (set! k c))
>    (printf "calling continuation...\n")
>    (k 'whatever)
>    (printf "done.\n"))

In PLT Scheme, there's a delimiting prompt around each top-level
expression. The `call/cc' function captures only up to the prompt, and
a continuation invocation replaces the current continuation only up to
the prompt.

Also, a top-level `begin' splices its sub-forms into the top-level, in
which case there is a separate prompt around each expression above. So,
`k' is bound to an empty continuation, and `(k whatever)' replaces the
empty continuation up to its prompt with the empty continuation --- so
that's why it just continues.

With `let', you have one top-level expression, and so you see the
expected loop: `k' is bound to a continuation that continues the let
body, and so invoking it restarts the body.


Posted on the users mailing list.