I’m having some trouble understanding this piece of code that my professor used as example:
(define saved-cont #f)
(define (test-cont)
(let ((x 0))
(call/cc
(lambda (k)
(set! saved-cont k)))
(set! x (+ x 1))
(display x)
(newline)))
If we run for the first time (test-cont) what does k contain?
Note: I’m using R6RS Scheme.
call/cccalls the given function with the current continuation as its sole argument. Thus,khere is the current continuation. When you call it with a value, thecall/ccwill return with the value you gave. (Though, since you’re not usingcall/cc‘s return value in your code above, and since R6RS allows zero-valued returns in that case, you can just callsaved-contwith no arguments and still do what you expect.)Here, basically, every time you call
(saved-cont), the code below thecall/ccwill run again. Thus,xwill increment, and its new value will display.