Chapter 13

Control and Exception Handling

Alice: “Would you tell me, please, which way I ought to go from here?”

The Cheshire Cat: “That depends a good deal on where you want to get to.”

— Lewis Carroll, Alice in Wonderland (1865)

Continuations are a very powerful tool, and can be used to implement both multiple processes and nondeterministic choice.

— Paul Graham, On Lisp (1993)

THIS chapter is about how control is fundamentally imparted to a program and how to affect control in programming. A programmer generally directs flows of control in a program through traditional control structures in programming languages, including sequential statements, conditionals, repetition, and function calls. In this chapter, we explore control and how to affect control in programming through the concepts of first-class continuations and continuation-passing style. An understanding of how control is fundamentally imparted to a program not only provides a basis from which to build new control structures (e.g., control abstraction), but also provides an improved understanding of traditional control structures.

We begin by introducing first-class continuations and demonstrating their use for nonlocal exits, exception handling, and backtracking. Then we demonstrate how to use first-class continuations to build other control abstractions (e.g., coroutines). Our discussion of first-class continuations for control leads us to issues of improving the space complexity of a program through tail calls and tall-call optimization. Tail calls leads to an introduction to continuation-passing style and CPS transformation. The CPS transformation supports iterative control behavior (i.e., constant memory space) without compromising the one-to-one relationship between recursive specifications/algorithms with their implementation in code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183