[Haskell-beginners] More simple continuation questions

John Wiegley johnw at newartisans.com
Wed Jul 16 03:06:03 UTC 2014


>>>>> martin  <martin.drautzburg at web.de> writes:

> (1) when reading about continuations, there is this thing which needs an
> "other function" to be passed to produce a
> final result. And there is this "other function". Which of the two is "the
> continuation"?

Say you have two functions, foo and bar:

    foo :: a -> b
    bar :: b -> c

Ordinarily we'd just compose these, like so:

    bar . foo

But there are times when this is not desired or possible.  In those cases, we
can change foo to accept a continuation instead:

    foo :: a -> (b -> r) -> r

'foo' doesn't know what type the continuation will want to return, nor should
it care.  So we just leave the result polymorphic, and call it 'r' for result.

Now we can call foo and pass it a "continuation": that is, the thing it should
do next with the value generated from 'foo':

    foo bar

Note that we can encode 'foo' a little more conveniently now as:

    foo :: a -> Cont r b

This allows us to use the CPS'd (continuation passing style) form of foo as a
Functor, Monad, etc.

John


More information about the Beginners mailing list