views:

212

answers:

1

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))
+3  A: 

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 set cont 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.

ivant
What does "#f" mean?
Chris Conway
@chris : false.
kunjaan
So in the three uses of cont in the example we have it: (1) being defined as a boolean value #f, (2) being set to a continuation value k, (3) being invoked as a function with a boolean argument #f?
Chris Conway
@Chris. Yup. But the #f is just a dummy value. We could have null or anything because at both times it is ignored.
kunjaan