[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

example of continuations

I find an example is often useful in these cases.  Here is
a simple one in T, a dialect of Scheme:

> (let ((x 0))
      (lambda (cc)
        (set frozen cc)))
    (format t "I am the line after the call/cc.~%")
    (set x (1+ x)))
I am the line after the call/cc.
> (frozen)
I am the line after the call/cc.
> (frozen)
I am the line after the call/cc.

In implementors' terms, a continuation is basically a program
counter and a copy of the stack.  You can invoke it like a
function, and you're back where you were when the continuation
was made.  So you can return multiple times out the same code.

It's not bad form to use the same continuation more than once.
That happens when you use continuations to do nondeterministic
search, for example.

Another thing continuations are useful for is server-based
apps, strangely enough.  We used them extensively in Viaweb
to fake subroutine-like behavior, though because we were
using CL rather than Scheme we used fake continuations made
out of closures.


--- Seth Gordon <sethg@ropine.com> wrote:
>    From: Dan Sugalski <dan@sidhe.org>
>    Oh, I see. A continuation is a function call where, not only do
> you get to 
>    decide what you return, you get to decide where you return it to.
>    That's evil. I really like it. :)
>    Are there general rules as to where you're allowed to return to? I
> presume 
>    picking PC addresses out of the air's considered bad form.
> In Scheme, the "call-with-current-continuation" form basically allows
> you to say, "I want other functions to be able to return their values
> *here* instead of to their callers", and then you can refer to
> "*here*" with the variable name of your choice, and then pass that
> variable to other functions.
> The bad form comes when you start doing things like using the same
> continuation more than once.
> -- 
> "If I don't know what I'm doing, I program in Scheme. If I don't want
> to be
> allowed to not know what I'm doing (possibly for the sake of
> efficiency or
> safety), I program in SML. If I want to get paid, I program in C++."
>   --Thant Tessman
> == Seth Gordon == sethg@ropine.com == http://ropine.com/ == std.
> disclaimer ==

Do You Yahoo!?
Send your FREE holiday greetings online!