Continuation Pattern

In FunctionalProgramming, a continuation is a function C that is passed to a function F, where the result of F is C applied to some intermediate result produced by F (see ContinuationExplanation). That is, F is defined to find some result then apply C to it. Hence, C tells F what to do with its result.

In procedural programming, callbacks are an example of a ContinuationPattern. Once a method has produced a result, its task is to invoke the callback with that argument.

I thought calling a continuation blows away the current stack. Calling a callback doesn't. What am I missing?

F is a HigherOrderFunction in this example.

But if all functions take a continuation C, then we don't have to pass a return address: a function call just becomes a jump with arguments. So in this view all functions are already higher-order, because all functions take a continuation (i.e. return address + local state ( = stack and registers and what can be reached from that.)) If we now additionally allow capturing of these ordinary implicit continuations, then we get (some variant of) CallWithCurrentContinuation.

But one does not have to use a language (implementation) that supports CallWithCurrentContinuation to be able to use ContinuationPassingStyle. It's just a bit more awkward (read : 'explicit').


And how does this compare with using monads (see OnMonads)?


Contributors: StefanLjungstrand


CategoryObjectFunctionalPatterns CategoryContinuation


EditText of this page (last edited March 29, 2005) or FindPage with title or text search