So for I am trying to figure out this whole call/cc
thing in Scheme. Below is the code I am working with:
(+ 1 (call/cc
(lambda (k)
(if (number? k)
(call/cc (lambda (k) (k (- 1 k))))
(k 4)))))
So here we start adding the two arguments in the first parenthesis. 1
and the rest which we have to evaluate because of eager evaluation. So we have a call/cc
which accepts one arguments, a function, which the call/cc
evaluates by calling. (am I right?) And at the same time it holds the rest of what has happened so far in our first parenthesis, namely (+ 1 [])
, which is the "continuation". (am I right?) So we call lambda k
with the continuation as I have just described, (+ 1 [])
. In the function it then asks if this is a number, which it is not and does the "then". I get "lost" here, what does this second call/cc
do? What is (k 4)
invoked, to make this whole thing evaluate to 5
?