Flow control is usually straightforward: sequence, selection, iteration. And many programmers, having been raised on these primary control structures, have a difficult time seeing what other kinds of flow control might be necessary. This article introduces continuations and teaches you to think about flow control in radically new ways.
Thielecke: Contrasting exceptions and continuations A very useful paper, analyzing the semantic differences between continuations (a la callcc) and exceptions. The paper is a tour de force, and the details can be quite daunting. However, the basic results are well worth remembering:
``Devils and Angels'', as defined in [Friedman, 84] is an exercise in continuation programming, which you hopefully might find amusing. This is a Haskell implementation of ``Devils and Angels'' in [Friedman, 84], with a small example. You can execute it in Hugs by first loading the project P, then evaluating the example. The implementation consists of the following files: P The Hugs project file Stack.hs A stack abstraction Cont.hs Provides a continuation monad, on top of a state monad with two stacks. Devils.hs Provides the devil primitives milestone, devil, and angel Example.hs A little example
Suppose someone stole all the monads but one, which monad would you want it to be? If you're a Haskell programmer you wouldn't be too bothered, you could just roll your own monads using nothing more than functions. But suppose someone stole do-notation leaving you with a version that only supported one type of monad. Which one would you choose? Rolling your own Haskell syntax is hard so you really want to choose wisely. Is there a universal monad that encompasses the functionality of all other monads? About a year ago I must have skimmed this post because the line "the continuation monad is in some sense the mother of all monads" became stuck in my head. So maybe Cont is the monad we should choose. This post is my investigation of why exactly it's the best choice. Along the way I'll also try to give some insight into how you can make practical use the continuation monad.
D. Quan, D. Huynh, D. Karger, and R. Miller. Proceedings of the 16th annual ACM symposium on User interface software and technology, page 145--148. Vancouver, Canada, ACM Press, (2003)