Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!das-news.harvard.edu!news2.near.net!MathWorks.Com!yeshua.marcam.com!charnel.ecst.csuchico.edu!olivea!news.hal.COM!decwrl!adobe!macb023.mv.us.adobe.com!user
From: mhamburg@mv.us.adobe.com (Mark Hamburg)
Subject: Re: definition of "Continuation"
Message-ID: <mhamburg-270994113839@macb023.mv.us.adobe.com>
Followup-To: comp.lang.scheme
Sender: usenet@adobe.com (USENET NEWS)
Organization: Adobe Systems, Inc.
References: <35rutm$a85@news.u.washington.edu> <BOBO.94Sep26110730@avogadro.arc.nasa.gov> <gat-260994131551@silicon.jpl.nasa.gov>
Date: Tue, 27 Sep 1994 19:38:39 GMT
Lines: 46

Another way to think about continuations is in terms of exit functions
(which can be built using continuations).  An exit function can be defined
using a syntax like the following:

(bind-exit <exit-function-name> <expr1> <expr2> <expr3>...)

The behavior of this construct is to execute the expressions in order with
<exit-function-name> bound to a function taking a single parameter which,
if called, will result in an immediate exit from the bind-exit expression
returning the value passed to the call.

Thus,

(bind-exit foo (+ 2 3) (foo 7) (print 'hello))

would evaluate to 7 without printing hello.

foo is bound to a function and this function can be passed as a parameter
in the expressions -- i.e., this does not define foo as a special keyword. 
This allows one to escape from arbitrarily deep call chains.

But what happens if we take this exit function and store it somewhere that
outlives the dynamic scope of the bind-exit expression?  In Dylan, an
invocation of the function would now be considered invalid.  In Scheme,
however, invocation of the function means return from the bind-exit
expression again into the dynamic context in which it was originally
evaluated.

For example:

(set! x (bind-exit foo (set! saved-foo foo) 2))

; x now contains 2

(saved-foo 3)

; x now contains 3

Clear as mud.

As for what they are useful for, that's another discussion.  Suffice it to
say that they are very useful for implementing co-routine packages,
exception
handling, and backtracking.

- Mark
