Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!das-news.harvard.edu!news2.near.net!MathWorks.Com!europa.eng.gtefsd.com!news.umbc.edu!haven.umd.edu!ames!kronos.arc.nasa.gov!usenet
From: bobo@avogadro.arc.nasa.gov (Mark Friedman)
Subject: Re: definition of "Continuation"
In-Reply-To: absinthe@viva.chem.washington.edu's message of 22 Sep 1994 12:56:22 GMT
Message-ID: <BOBO.94Sep26110730@avogadro.arc.nasa.gov>
Lines: 86
Sender: usenet@ptolemy-ethernet.arc.nasa.gov (usenet@ptolemy.arc.nasa.gov)
Nntp-Posting-Host: avogadro.arc.nasa.gov
Reply-To: bobo@ptolemy.arc.nasa.gov
Organization: NASA/Ames Information Sciences
References: <35rutm$a85@news.u.washington.edu>
Date: Mon, 26 Sep 1994 18:07:30 GMT

In article <35rutm$a85@news.u.washington.edu>
absinthe@viva.chem.washington.edu (Daniel Faken) writes:

   Could somebody give a definition of what a "continuation" is?

I'm not sure if the poster has seen the r4rs description of
continuations. I'm assuming that he has and still would like some more
explanation and perhaps an example. I'll append the r4rs description
at the end anyway, in case he (or other readers) hasn't seen it. It's
a pretty good, concise description.

A continuation represents what will be done with the result of a
procedure call. Thus if you typed in the following (in a typical
Scheme implementation):

  > (+ 10 (- 4 1))
  > ;result -> 13

The continuation of the procedure call "(- 4 1)" in this context would
be to add 10 to it and print out the result.

In CALL-WITH-CURRENT-CONTINUATION continuations
are reified (i.e. made into a Scheme object) into a procedure. So, for
example, to reify the  continuation of "(- 4 1)" as above
we could do:

  > (+ 10 (call-with-current-continuation
  >         (lambda (kontinuation)
  >           (- 4 1))))
  > ;result -> 13

Which would represent the same computation as "(+ 10 (- 4 1))" except
that we have reified (into the variable named "kontinuation") the
continuation of "(- 4 1)". Note, however, that we haven't done
anything with the refied continuation. We could, for example, capture
it:

  > (define my-kont)
  > (+ 10 (call-with-current-continuation
  >         (lambda (kontinuation)
  >           (set! my-kont kontinuation)
  >           (- 4 1))))
  > ;result -> 13

Now we actually have (as the value of my-kont) a procedure which
represents our continuation. It ought the be a procedure a takes a
value (in this case 3, i.e. "(- 4 1)"), adds 10 to it and prints out
the result. So let's try it:

  > (my-kont 100)
  > ;result -> 110

Note that once the continuation is "freed" from it's creator it no
longer has to be the continuation of "(- 4 1)". It will now take any
value, add 10 to it and print the result.

As promised, here is the description of continuations given in the
r4rs as a footnote to the CALL-WITH-CURRENT-CONTINUATION procedure:

     Whenever a Scheme expression is evaluated there is a
     "continuation" wanting the result of the expression.  The
     continuation represents an entire (default) future for the
     computation.  If the expression is evaluated at top level, for
     example, then the continuation might take the result, print it on
     the screen, prompt for the next input, evaluate it, and so on
     forever.  Most of the time the continuation includes actions
     specified by user code, as in a continuation that will take the
     result, multiply it by the value stored in a local variable, add
     seven, and give the answer to the top level continuation to be
     printed.  Normally these ubiquitous continuations are hidden
     behind the scenes and programmers don't think much about them.
     On rare occasions, however, a programmer may need to deal with
     continuations explicitly. `Call-with-current-continuation' allows
     Scheme programmers to do that by creating a procedure that acts
     just like the current continuation.


-Mark
-- 
Mark Friedman
NASA-Ames Research Center
MS 269-2
Moffett Field, CA 94035-1000

vmail: (415) 604-0573
email: bobo@ptolemy.arc.nasa.gov
