15-150: Principles of Functional Programming

Lecture 12: Continuations

Continuations act as "functional accumulators." The basic idea is to implement a function f by defining a tail-recursive function f' that takes an additional argument, called the continuation. This continuation is a function; it encapsulates the computation that should be done on the result of f. In the base case, instead of returning a result, one calls the continuation. In the recursive case, one augments the given continuation with whatever computation should be done on the result.

Continuations can be used to advantage for programming solutions to a variety of problems. In today's lecture we look at two examples. First, we implement a function for summing the integers in a list using continuations (and compare that to other possible implementations). We also look at an example in which continuations may be used to efficiently manage evaluations, short-circuiting unnecessary computations. Next time we will look at the use of continuations in backtracking search.

Key Concepts

Sample Code

Some Notes on Continuations

Additional Notes on Continuations