Continuation of the scheme. What's the difference between calling "call / cc" at the top level and not at the top level?
This code works as expected:
(define saved #f)
(cons 'wo (call/cc (lambda (k) (set! saved k) '())))
(saved 'ca!)
(racket console):
'(wo)
'(wo . ca!)
But when I move it into a function and call it, the program never stops. Why?
(define (test)
(define saved #f)
(cons 'wo (call/cc (lambda (k) (set! saved k) '())))
(saved 'ca!))
(test)
+3
source to share
1 answer
A continuation is all that's left to do in the execution context where it was saved.
In the first case, the continuation persists across the call cons
, so it just minuses the wo to something and falls back to the REPL.
In the second case, you are calling the procedure test
, so the continuation is
- cons "wo to something
- call the procedure associated with
saved
(i.e. continuation) with
so the continuation calls itself, hence the cycle.
+4
source to share