[plt-scheme] Understanding continuations
I suggest you use DrScheme debugger to see what happens with:
> #lang scheme
> (define cont #f)
> (define (foo)
> (call/cc
> (lambda (arg-cont)
> (set! cont arg-cont)
> 13)))
> (+ 1 2 3 (foo))
(cont 4)
Do you see why this happens?
Jos
----- Original Message -----
From: "Grant Rettke" <grettke at acm.org>
To: "PLT-list" <plt-scheme at list.cs.brown.edu>
Sent: Saturday, July 19, 2008 7:44 AM
Subject: [plt-scheme] Understanding continuations
> 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?
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme