The duplication is because you're evaluating the expression at top
level, so the repl is part of the continuation. The continuation isn't
(lambda (c) (c e2); it's actually something like (lambda (c)
(evaluate-in-repl (c e2)). So when you run (ret 9), you're actually
re-running the repl you had when you evaluated the expression, which
leads to the duplication.
To avoid this, put the expression in a procedure:
#lang racket
(define ret #f)
(define ret2 (lambda (c) (add1 c)))
(define (haha)
(add1
(call/cc
(lambda (k)
(set! ret k) ;; Now ret should be equivalent to ret2.
2))))
Then you get what you expect, because the continuation is delimited by
the procedure definition:
Welcome to DrRacket, version 7.8 [3m].
Language: racket, with debugging; memory limit: 128 MB.
> (haha)
3
> (ret 9)
10
> (ret2 5)
6
On Mon, Dec 7, 2020 at 9:26 PM Tim Meehan <[email protected]> wrote:
>
> I've read a lot about call/cc, and each time wind up just moving on. So this
> is an early New Year's resolution: getting a better understanding of it.
>
> According to Wikipedia's page on continuations, the continuation of the
> statement:
>
> ((call/cc f) e2)
>
> is:
>
> (lambda (c) (c e2))
>
> #lang racket
> (define ret #f)
> (define ret2 (lambda (c) (add1 c)))
>
> (add1
> (call/cc
> (lambda (k)
> (set! ret k) ;; Now ret should be equivalent to ret2.
> 2)))
>
> (ret 9) ;; Why does this print twice?
> (ret2 5) ;; This only prints once, like I would have expected.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CACgrOx%2BnXZpQv_MbDwdAZJSGpMiD%2BE1gB4YH8Rsb_y3%3D6RaAnQ%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/CA%2B80D0XpFgieuANWieS7NEoe%3DOHweZXsgB9wjvQmg1j-aggumQ%40mail.gmail.com.