115 lines
6.8 KiB
HTML
115 lines
6.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
|
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
|
<HEAD>
|
|
<TITLE>7. Control Structure</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Control Structure">
|
|
<meta name="keywords" value="clm">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<b>Common Lisp the Language, 2nd Edition</b>
|
|
<BR> <HR><A NAME=tex2html2456 HREF="node77.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2454 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2448 HREF="node75.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2458 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2459 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html2457 HREF="node77.html"> Constants and Variables</A>
|
|
<B>Up:</B> <A NAME=tex2html2455 HREF="clm.html">Common Lisp the Language</A>
|
|
<B> Previous:</B> <A NAME=tex2html2449 HREF="node75.html"> Logical Operators</A>
|
|
<HR> <P>
|
|
<H1><A NAME=SECTION001100000000000000000>7. Control Structure</A></H1>
|
|
<P>
|
|
<A NAME=CONTRL>Common</A>
|
|
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.
|
|
<P>
|
|
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.
|
|
<P>
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
Locally defined functions (<tt>flet</tt>, <tt>labels</tt>) and macros (<tt>macrolet</tt>)
|
|
are quite versatile.
|
|
The new symbol macro facility allows even more syntactic flexibility.
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
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
|
|
<tt>progn</tt>, which is roughly equivalent to an Algol <b>begin</b>-<b>end</b>
|
|
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 <tt>progn</tt>.''
|
|
<A NAME=8117> </A>
|
|
Other sequencing constructs include <tt>prog1</tt> and <tt>prog2</tt>.
|
|
<P>
|
|
For looping, Common Lisp provides the general iteration facility
|
|
<tt>do</tt> as well as a variety
|
|
of special-purpose iteration facilities for iterating or mapping
|
|
over various data structures.
|
|
<P>
|
|
Common Lisp provides the simple one-way conditionals <tt>when</tt> and <tt>unless</tt>,
|
|
the simple two-way conditional <tt>if</tt>, and the more general multi-way
|
|
conditionals such as <tt>cond</tt> and <tt>case</tt>. The choice of which form
|
|
to use in any particular situation is a matter of taste and
|
|
style.
|
|
<P>
|
|
Constructs for performing non-local exits with various scoping
|
|
disciplines are provided: <tt>block</tt>, <tt>return</tt>,
|
|
<tt>return-from</tt>,
|
|
<tt>catch</tt>, and <tt>throw</tt>.
|
|
<P>
|
|
The multiple-value constructs provide an efficient way for a function
|
|
to return more than one value; see <tt>values</tt>.
|
|
<P>
|
|
<HR>
|
|
<UL>
|
|
<LI> <A NAME=tex2html2460 HREF="node77.html#SECTION001110000000000000000"> Constants and Variables</A>
|
|
<UL>
|
|
<LI> <A NAME=tex2html2461 HREF="node78.html#SECTION001111000000000000000"> Reference</A>
|
|
<LI> <A NAME=tex2html2462 HREF="node79.html#SECTION001112000000000000000"> Assignment</A>
|
|
</UL>
|
|
<LI> <A NAME=tex2html2463 HREF="node80.html#SECTION001120000000000000000"> Generalized Variables</A>
|
|
<LI> <A NAME=tex2html2464 HREF="node81.html#SECTION001130000000000000000"> Function Invocation</A>
|
|
<LI> <A NAME=tex2html2465 HREF="node82.html#SECTION001140000000000000000"> Simple Sequencing</A>
|
|
<LI> <A NAME=tex2html2466 HREF="node83.html#SECTION001150000000000000000"> Establishing New Variable Bindings</A>
|
|
<LI> <A NAME=tex2html2467 HREF="node84.html#SECTION001160000000000000000"> Conditionals</A>
|
|
<LI> <A NAME=tex2html2468 HREF="node85.html#SECTION001170000000000000000"> Blocks and Exits</A>
|
|
<LI> <A NAME=tex2html2469 HREF="node86.html#SECTION001180000000000000000"> Iteration</A>
|
|
<UL>
|
|
<LI> <A NAME=tex2html2470 HREF="node87.html#SECTION001181000000000000000"> Indefinite Iteration</A>
|
|
<LI> <A NAME=tex2html2471 HREF="node88.html#SECTION001182000000000000000"> General Iteration</A>
|
|
<LI> <A NAME=tex2html2472 HREF="node89.html#SECTION001183000000000000000"> Simple Iteration Constructs</A>
|
|
<LI> <A NAME=tex2html2473 HREF="node90.html#SECTION001184000000000000000"> Mapping</A>
|
|
<LI> <A NAME=tex2html2474 HREF="node91.html#SECTION001185000000000000000"> The ``Program Feature''</A>
|
|
</UL>
|
|
<LI> <A NAME=tex2html2475 HREF="node92.html#SECTION001190000000000000000"> Structure Traversal and Side Effects</A>
|
|
<LI> <A NAME=tex2html2476 HREF="node93.html#SECTION0011100000000000000000"> Multiple Values</A>
|
|
<UL>
|
|
<LI> <A NAME=tex2html2477 HREF="node94.html#SECTION0011101000000000000000"> Constructs for Handling Multiple Values</A>
|
|
<LI> <A NAME=tex2html2478 HREF="node95.html#SECTION0011102000000000000000"> Rules Governing the Passing of Multiple Values</A>
|
|
</UL>
|
|
<LI> <A NAME=tex2html2479 HREF="node96.html#SECTION0011110000000000000000"> Dynamic Non-Local Exits</A>
|
|
</UL>
|
|
<BR> <HR><A NAME=tex2html2456 HREF="node77.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2454 HREF="clm.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2448 HREF="node75.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2458 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2459 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html2457 HREF="node77.html"> Constants and Variables</A>
|
|
<B>Up:</B> <A NAME=tex2html2455 HREF="clm.html">Common Lisp the Language</A>
|
|
<B> Previous:</B> <A NAME=tex2html2449 HREF="node75.html"> Logical Operators</A>
|
|
<HR> <P>
|
|
<HR>
|
|
<P><ADDRESS>
|
|
AI.Repository@cs.cmu.edu
|
|
</ADDRESS>
|
|
</BODY>
|