Search code examples
lambdaschemeracketcallcc

How does this call/cc expression work?


I am trying this on Racket and it gives out the answer as 5. But I cannot seem to figure out how it got to the answer.

((call/cc call/cc) (lambda (x) 5)) 

I expanded it as follows.

((call/cc (lambda (k) (call/cc (lambda (k1) (k k1))))) (lambda (x) 5))

Assuming the expansion is correct I still don't understand what happens when k continuation is applied to k1 continuation and how it does to the execution of outer lambda to yield 5.


Solution

  • The (k k1) would return k1 as the return value of the outer call/cc. Then when you invoke k1 (as part of (... (lambda (x) 5))), that returns 5 as the return value of the inner call/cc, which is then returned (as a normal return this time) as the return value of the outer call/cc.

    Sorry, that was quite a mouthful. :-)