[plt-scheme] Continuations and (begin)

From: James Coglan (jcoglan at googlemail.com)
Date: Sat Feb 7 08:34:38 EST 2009

Hi all,

Say I have the following:

(define r #f)
(define count-calls 0)

(begin
  (call/cc
    (lambda (k)
      (set! r k)
      #t))
  (set! count-calls (+ count-calls 1)))
(r #t) (r #t) (r #t)

I would expect count-calls to equal 4 after this, but it equals 1. The
continuation, from what little I've read about this, should look like:

([#procedure] [#hole] (set! count-calls (+ count-calls 1)))

where 'begin' has been evaluated, but the (set!) expression has not. Surely
picking up again from #hole should call (set!) each time? I could understand
this behaviour if I'd instead written:

(begin
  (set! count-calls (+ count-calls 1))
  (call/cc
    (lambda (k)
      (set! r k)
      #t)))
(r #t) (r #t) (r #t)

Then the continuation would be:

([#procedure] [#undefined] [#hole])

Could someone explain what's going on, and tell me whether there are other
exceptions I'd be surprised by? I thought maybe it was saving the state of
all variables with the continuation, but this would seem to refute that:

(set! count-calls 0)
(define (foo)
  (call/cc
    (lambda (k)
      (set! r k)
      #t))
  (set! count-calls (+ count-calls 1)))
(foo)
(r #t) (r #t) (r #t)

; count-calls = 4

-- 
James Coglan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090207/93fbf44a/attachment.html>

Posted on the users mailing list.