CS 15-212: Fundamental Structures of Computer Science II |
Continuations act as "functional accumulators." The basic idea of the
technique is to implement a function f
by defining a
tail-recursive function f'
that takes an additional
argument, called the continuation, which represents the
computation that should be done on the result of f
. In
the base case, instead of returning a result, we call the
continuation. In the recursive case we add whatever computation should
be done on the result to the continuation.
Continuations can be used to advantage for programming solutions to a variety of problems. In today's lecture we'll look at a simple example where continuations are used to efficiently manage a certain pattern of control. We'll see a related and more significant example in an upcoming lecture when we look at regular expressions.