0

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.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
budchan chao
  • 327
  • 3
  • 15

1 Answers1

1

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. :-)

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Just to confirm my understanding of the explanation. (k k1) returns with k1. Then (k1 (lambda (x) 5)) happens. Which returns with (lambda (x) 5). Then ((lambda (x) 5) (lambda (x) 5)) happens yielding 5. Did I get it correct? – budchan chao Dec 12 '14 at 14:11
  • Yep, that sounds reasonable. – C. K. Young Dec 13 '14 at 03:28
  • Awesome. So in this case both k and k1 continuations are pretty much the same right? – budchan chao Dec 13 '14 at 03:54
  • They are not the same continuations (one returns to the outer `call/cc`'s caller, the other returns to the inner `call/cc`'s caller), but their effects are similar enough that you could consider it that way. – C. K. Young Dec 13 '14 at 03:59