If a continuation captures the rest of a computation, why doesn't the following snippet of Scheme code enter into an infinite loop?
(define cont #f)
(define (funny1)
(print "entered funny")
(call/cc (lambda (k) (set! cont k)))
(print "left funny"))
(funny1)
(print "end")
(cont)
It seems like returning from the function and calling 'print' and 'cont' should be part of the rest of the computation, triggering an infinite loop. Instead, it simply prints: "entered funny", "left funny", "end", "left funny"
The following CPS transformation of the previous snippet does, indeed, enter an infinite loop as expected:
(define cont #f)
(define (funny2 ret)
(print "entered funny")
(let ((rest (lambda ()
(print "left funny")
(ret))))
(set! cont rest)
(rest)))
(funny2 (lambda ()
(print "end")
(cont))))
What am I missing here?