Next: Macros
Up: Misellaneous Topics
Previous: Truth Values
Although a Lisp program is a sequence of expressions, life would be quit boring
if we could not, somehow, alter the flow of control to go ``non-linearly''.
Lisp has almost all the usual control constructs one finds in other high level
languages, like varieties of sequencing, conditional contructs, iteration, and
some form of ``jumps''. The one missing contruct is the for loop. As
we have already seen in an earlier section, all of these contructs are
implemented as special forms. Table 4 shows some of the
control constructs available and some examples.
Table 4:
A representative sample of various types of control contructs
available in Lisp.
| progn |
Sequential evaluation. Used when you |
(progn (message ``1.'') |
| |
really want multiple forms where only one |
(message ``2.'') |
| |
is allowed |
(message ``3'')) |
if |
The usual thing |
(if condition |
| |
|
then-form |
| |
|
else-forms) |
when |
Just the 'if' portion of an if contruct but |
(when condition A B C) |
| |
allows multiple forms |
|
unless |
The else portion of the if construct |
(unless condition A B C) |
|
|
|
and |
Combining conditionals to build |
(and A B C ... ) |
| or |
complex conditional |
(or A B C ...) |
| not |
|
(not A) |
while |
Iteration |
(while condition forms...) |
|
|
|
|
Next: Macros
Up: Misellaneous Topics
Previous: Truth Values
Sriram Karra
2005-01-06