Thursday, August 07, 2003

Continuations in R5RS

I read about continuations in the R5RS the other day, and it made a lot of sense. I will attempt to describe my understanding of it now.

Call-with-current-continuation (call/cc) takes one argument, which should be a procedure, lets call it "proc-a", which itself takes one argument. Remember that in Scheme, procedures are objects that can be assigned to variables and passed as arguments. Call/cc then calls proc-a, passing it an argument which is called "the continuation". Proc-a can now do whatever it wants with the continuation, storing it in a variable, passing it to other procedures, or whatever. At any later point, the continuation itself can be called as a procedure, which results in the call state of the program rolling back to what it was at the invocation of the call/cc. It is similar to setjmp/longjmp in C, but my understanding is that it is much more powerful. I'm sure that understanding and appreciating the implications of that power will take me a while.

No comments: