In the book Lisp in Small Pieces, there is the following example code, which is intended to demo that call/cc
could simulate goto.
(define (fact n)
(let ((r 1) (k 'void))
(call/cc (lambda (c) (set! k c) 'void))
(set! r (* r n))
(set! n (- n 1))
(if (= n 1) r (k 'recurse))))
However, I'm not sure if I'm misunderstanding something, but I cannot see that this is the way call/cc
would simulate goto. When k
is applied in the last line, the restored continuation has the r
and n
of the original continuation, whose values are not changed by the two set!
applications. So the entire loop will never terminate.
Is the book wrong in this example? Or did I miss anything?