[racket] Call/cc captures lexical continuation?...

From: Rüdiger Asche (rac at ruediger-asche.de)
Date: Wed Jan 18 08:32:52 EST 2012

Well, idiot me. First question and I already goofed up - didn't take  
tail recursion optimization into consideration. this here does the  
trick. sorry for the noise.

(letrec
   [
    (emulated-times (lambda (accumulated-result rest multiplicator)
                         (if (eq? rest 0)
                             accumulated-result
                             (begin
                                (display accumulated-result)
                                (display " ")
                                (display rest)
                                (display "\r\n")
                                (if (eq? rest 10)
                                    (+ accumulated-result (call/cc  
(lambda (x) (set! glbSnapshot x)
                                                             
(emulated-times multiplicator (- rest 1) multiplicator))))
                                (emulated-times (+ accumulated-result  
multiplicator) (- rest 1) multiplicator)
                              )))))
    (et-displayed (lambda (multiplicand multiplicator)
                     (display (emulated-times 0 multiplicand multiplicator))
                     (display "\r\n")))]
    (et-displayed 12 4)
)




Quoting Rüdiger Asche <rac at ruediger-asche.de>:

> Hello, I'm trying to brush up on my scheme/racket, and here's something
> I came across when trying to rework the issue of continuations - I'm
> sure this qualifies as a beginner's question, so sorry for probably
> asking a stupid question -  I promise to ask better ones in the future:
>
> Consider the following code:
>
> (define glbSnapshot 0)
>
> (letrec
>   [
>    (emulated-times (lambda (accumulated-result rest multiplicator)
>                         (if (eq? rest 0)
>                             accumulated-result
>                             (begin
>                                (display accumulated-result)
>                                (display " ")
>                                (display rest)
>                                (display "\r\n")
>                                (if (eq? rest 10)
>                                    (call/cc (lambda (x) (set! glbSnapshot x)
>                                                         (emulated-times
> (+ accumulated-result multiplicator) (- rest 1) multiplicator)))
>                                (emulated-times (+ accumulated-result
> multiplicator) (- rest 1) multiplicator)
>                              )))))
>    (et-displayed (lambda (multiplicand multiplicator)
>                     (display (emulated-times 0 multiplicand multiplicator))
>                     (display "\r\n")))]
>    (et-displayed 12 4)
> )
>
> I would expect glbSnapshot to be the continuation accumulated upon the
> second recursion, i.e. whatever is passed to the continuation would
> unwind the stack and complete the partial computation already done when
> taking the snapshot, thus:
>
> (glbSnapshot 7)  => 8+7 => 15
>
> However,
>
> (glbSnapshot 7)  => 7 on Racket 5.1.3. It seems as if the continuation
> is only taken at the lexical, not the dynamic time! Somehow I was under
> the impression that call/cc would express the "true" continuation when
> taken, but either I'm wrong or Racket 5.1.3. implements it wrong...
> Almost certainly it's my misunderstanding - thus, what do I have to do
> to capture the dynamic continuation?
>
> Thanks!
>
> - Ruediger (IU alumnus and spare time Scheme afficionado)
>
>
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users





Posted on the users mailing list.