views:

70

answers:

1

(callcc (fun k -> k 7)) + 3
(callcc (fun k -> 7)) + 3

What do each of these evaluate to and why?

+1  A: 

I guess this is homework. If it's not, just say so and I'll tell you the answer directly.

The way call/cc works it to capture the continuation at the point it's called. What that means for these examples, is when you see call/cc, replace the whole call with a black box and look at what's left:

(call/cc (fun k -> k 7)) + 3
=>
************************ + 3

So + 3 is what happens with the result of the call/cc call. This "what happens next" is the thing that call/cc packages up and calls k*.

All you need to now is figure out what happens with you call k with the value 7.

For the second example, you don't call k at all. Since you don't do anything special with k, you shouldn't expect call/cc to do anything special.


Note: The code you give looks like some kind of ML. None of the ML dialects I know have call/cc, so if your dialect doesn't either, try downloading PLT Scheme to play with it interactively. The syntax isn't too hard to pick up.

*'continuation' is a slightly nicer way to say "what happens next", although not much nicer.

Nathan Sanders