Can somebody please explain this continuation in scheme?
-
22-07-2019 - |
Question
I am learning continuations but I can't wrap my head around this code. Why does it go into infinite loop?
(let ((cont #f))
(call/cc (lambda (k)
(set! cont k)))
(cont #f))
Solution
Line by line:
- we define local variable
cont
- we call with current continuation some lambda function, that is, we are passing the current continuation to the lambda function as an argument. Current continuation is a function of 1 argument, this argument is then used as a "return value" of
call/cc
(and here is eventually ignored). - So
k
here signifies everything, that will happen after, in our case something close in sense to(lambda (call/cc-retval) (let () call/cc-retval (cont #f)))
. We setcont
value to this continuation.call/cc
returns. - Since
cont
is now a function which represented the continuation, and it's argument is the "return value of call/cc", we just call that function, the argument gets ignored, and we need to call the(cont #f)
again.
Thus, what we get eventually is infinite loop.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow