I don't understand how the following does not set up an infinite loop:
(define call/cc call-with-current-continuation) ; ccc alias
(define return #f) ; declare a global variable 'return'
(+ 1 (call/cc (lambda (cont) ; setup continuation with 'cont' as the exit procedure
(set! return cont) ; set global var 'return' to the exit procedure
1)))
(return 22) ; 23
When I call (return 22)
, I jump back to the continuation, but with the passed value, 22, as the new evaluated result of the call/cc
form. Would that not result in (return 22)
being evaluated as the next statement, thus setting up an infinite loop?
I know it is not an infinite loop, but I do not understand why it is not.
It is an infinite loop. However, in most Scheme implementations, top-level forms are evaluated in prompts.
If you put your expressions inside a
(let () ...)
, say, you'll definitely see the infinite loop in effect.