Continuations and threads (was Re: Iterators & generators)

jeremy at cnri.reston.va.us jeremy at cnri.reston.va.us
Fri Feb 18 10:11:48 EST 2000


In article <lY_q4.65559$up4.1046310 at news1.rdc1.ab.home.com>,
  nascheme at enme.ucalgary.ca (Neil Schemenauer) wrote:

> Here is my two bit explanation (based on Scheme's call/cc):
>
> A continuation is a function which represents the rest of the
> program.  Instead of returning from a function you can call this
> function giving it one argument which is the return value.
>
> These functions are first class just like everything else in
> Scheme.  You can bind them to variables or pass them as
> arguments.  You can also call them more than once passing
> different "return" values.
>

It may help to describe, in general terms, what you can do with a first
class continuation.  Here's my two bit followup to your explanation.

Each time you call a function, there is an implicit continuation -- the
place where the function should return to.  call/cc provides a way to
bind that implicit continuation to a name and keep hold of it.

If you don't do anything with the named continuation, the program will
eventually invoke it anyway.  The function returns and passes control to
its continuation.  Now that it's named, however, you could invoke the
continuation earlier than normal, or you could invoke it later (after it
has already been invoked by the normal control flow).  In the early
case, the effect is a non-local exit.  In the later case, it's more like
returning from the same function more than once.

Jeremy


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list