Common Lisp the Language, 2nd Edition

next up previous contents index
Next: Constants and Variables Up: Common Lisp the Language Previous: Logical Operators

7. Control Structure

Common Lisp provides a variety of special structures for organizing programs. Some have to do with flow of control (control structures), while others control access to variables (environment structures). Some of these features are implemented as special forms; others are implemented as macros, which typically expand into complex program fragments expressed in terms of special forms or other macros.

Function application is the primary method for construction of Lisp programs. Operations are written as the application of a function to its arguments. Usually, Lisp programs are written as a large collection of small functions, each of which implements a simple operation. These functions operate by calling one another, and so larger operations are defined in terms of smaller ones. Lisp functions may call upon themselves recursively, either directly or indirectly.

Locally defined functions (flet, labels) and macros (macrolet) are quite versatile. The new symbol macro facility allows even more syntactic flexibility.

While the Lisp language is more applicative in style than statement-oriented, it nevertheless provides many operations that produce side effects and consequently requires constructs for controlling the sequencing of side effects. The construct progn, which is roughly equivalent to an Algol begin-end block with all its semicolons, executes a number of forms sequentially, discarding the values of all but the last. Many Lisp control constructs include sequencing implicitly, in which case they are said to provide an ``implicit progn.''   Other sequencing constructs include prog1 and prog2.

For looping, Common Lisp provides the general iteration facility do as well as a variety of special-purpose iteration facilities for iterating or mapping over various data structures.

Common Lisp provides the simple one-way conditionals when and unless, the simple two-way conditional if, and the more general multi-way conditionals such as cond and case. The choice of which form to use in any particular situation is a matter of taste and style.

Constructs for performing non-local exits with various scoping disciplines are provided: block, return, return-from, catch, and throw.

The multiple-value constructs provide an efficient way for a function to return more than one value; see values.

next up previous contents index
Next: Constants and Variables Up: Common Lisp the Language Previous: Logical Operators