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

Re: Continuations with pictures



Aaah, too many analogies! :)  I've delved into the subject months ago, and
now understand how simple they are:

Instead of returning from a function and starting over, continuations simply
continue from where they left off.

add1() {
  loop {
    a += 1
    yield a  // return here, but also come back here
  }
}

print add1() -> 1
print add1() -> 2

Continuations require a persistent stack since the variables don't go out of
scope like they do in a return.  Continuations are the basis for coroutines
(two functions calling each other) and user level threads.

~ Mike ~

"Kimberley Burchett" <kimbly@cybercom.net> wrote in message
Pine.LNX.4.21.0308081045010.4093-100000@shell.cybercom.net">news:Pine.LNX.4.21.0308081045010.4093-100000@shell.cybercom.net...
>
> On Fri, 8 Aug 2003, Kevin Kelleher wrote:
>
> > So here is a picture -- maybe you can tell me what's wrong
> > with it:
>
> It seems like you're thinking of all the junk in the room as being part of
> the continuation.  But it would probably be more appropriate to think of
> the junk as global variables, that would still be wherever they were even
> after you invoked the continuation...
>
> I think it would be easier to offer a slightly different picture rather
> than try to fix up bit by bit the one that you illustrated.
>
> A continuation is more like a little envelope containing instructions
> about what to do next.
>
> Imagine that right after your daughter cleaned up her room, she wanted to
> go get some ice cream as a reward.  She knows that she's prone to forget
> these things so she decides to write a note to remind herself (yes, it's
> very odd for a child to not only forget the reward, but also have the
> foresight to write it down, but bear with me).  On the outside of the
> envelope, she writes "Do this next".
>
> Now her friends come over, and her room gets messed up again.  Time
> passes...
>
> The next morning, she wakes up and in the midst of the mess, she notices
> an envelope labeled "Do this next".  She opens it, and it tells her to go
> get ice cream.  She thinks this sounds like a good idea, so she does.
>
> Notice that she didn't put the room back in order first.  The room is all
> the global variables -- nothing happens to them simply by invoking the
> continuation.  Basically, you can think of the continuation as a "train of
> thought".  When she wrote down the note (created the continuation), she
> had intended to get ice cream.  She then promptly forgot this plan, so
> that train of thought disappeared.  But then she read the note (invoked
> the continuation) and the train of thought was resumed.  Notice also that
> the original motivation for getting ice cream was as a reward for having
> cleaned up the room -- but the fact that the room is now messed up again
> doesn't prevent her from getting ice cream anyway.
>
> The label on the envelope is arbitrary -- it's simply the name of the
> variable that holds the continuation.  You could label it anything you
> wanted, but it would still have the effect of changing your train of
> thought.
>
> There's no such thing as a future continuation, since nobody has yet had
> that train of thought, so they can't write it down.  There is a difference
> between upward and downward continuations, but the difference is mostly a
> matter of implementation and how you use them.
>
> I didn't address all the issues involved in continuations -- e.g. where
> they "return to", but I think this conveys the gist of the concept at the
> same level that you were originally asking about.
>
> --
> Kimberley Burchett
> http://www.kimbly.com/
>
>