Both closures and continuations capture the environment that was in
effect at the time of their construction. That environment is restored
when a closure or a continuation is invoked.
If you program in a direct style you expect that an application of a
closure or a procedure will return, with the result. When you invoke a
captured continuation however, you do not expect any result since an
invoked continuation never returns. The difference is that between
CALL and GOTO.
When you program in a continuation-passing style (CPS) you don't
expect any procedure to return either. No call ever returns. In that
case, the difference between closures and continuations disappears --
which is precisely the point of the CPS.
A continuation can be called a closure for "the rest of the program".
I must point out to a comprehensive and engrossing talk John
C. Reynolds gave at the 2nd ACM SIGPLAN Workshop on Continuations
(CW'97): "The discoveries of continuations".
http://www.brics.dk/~cw97/ProceedingS/06.ps.gz
This is a classical paper that deserves to be referenced in the
corresponding section of LtU. Note that van Wijngaarden devised the
continuation-passing style back in 1964, as a tool to eliminate all
gotos and labels from Algol. It was this talk about redundancy of goto
that inspired Dijkstra to consider practical aspects of programming
without goto. As one participant remembers, "Dijkstra spent that
evening [after the talk] constructing realistic examples of programs
without goto's, which he scribbled on napkins at coffee break the next
day." These napkins eventually turned into the famous letter to the
editor.
|