Hacker News new | past | comments | ask | show | jobs | submit login

So continuations are like the yield statement? They are basically a state machine?

Or are they like tail calls?

Yeah, this is wrong. continuations close over the heap. set! will have visible effects.

---------- original -----------

They save the state of your whole program. It's just like a checkpoint in a video game. (call/cc try-the-left-door) is like doing a quick save before doing something crazy.

Say you've got a big complicated data structure, and you need to make a bunch of destructive changes to it like a stack of 3d model transformations. You have a user sitting there watching a progress bar fill up. One approach is to just go for it, and make the changes. If the user clicks cancel, you have to undo all the work you've done so far.

Another approach is to deep copy the structure, and do your changes on the copy. If the user clicks cancel, you just throw away the modified copy.

the call/cc approach let's you skip doing the whole copy manually. The underlying system will save the state of your whole program, so you can change whatever you want. If the user clicks cancel, you call/cc and it'll restore the state of your program.

Perhaps you have two libraries. in a very dynamic language like ruby, you could call/cc, load up a library, monkey patch it, use it, see it didn't parse the way you wanted it to, and then call the continuation. You jump back to your last save point. It's as though you never loaded or modified the library, and then try the other library.

they get weird, because after you tried that first library, but before you gave up, you could take another quicksave of your program from that point. you can have as many save points as you want. so you try out the second library, decide to try one more thing back in the world where you'd tried out the first approach.

It's kinda sorta like, if some of your character's inventory was preserved across saves. save the game, go down the left hall and get the key. load the game go down the right hall and use the key (because you get to keep some things across savepoints). save again and grab the mystic idol. load up that first save, and use the idol.

They're really really neat. they're not really tricky, but you kind of have to know everything your program is doing.

Well, a call/cc saves the stack, but not the state. (My "VM snapshot" explanation was very bad in this regard.) If you allocate some memory and set it to a value, then save a continuation and change that value, it won't rewind to the old value when you invoke the continuation since the value isn't a part of the stack.

Your points are true if you save that complicated data structure as local variables on the stack, though.

Well, neither. It's sort of like a yield statement for the whole stack. But that metaphor is more confusing than helpful.

It basically just saves the whole call stack and lets you restore exactly that stack anytime you want. So it's like a longjmp, but also "jumps" to whatever function return chain was in progress at the time the continuation was saved.

If it seems weird, it is. But with weirdness comes power.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact