emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node222.html

190 lines
10 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!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>24.3. Special Forms for Exhaustive Case Analysis</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Special Forms for Exhaustive Case Analysis">
<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=tex2html4323 HREF="node223.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4321 HREF="node219.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4317 HREF="node221.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4325 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4326 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4324 HREF="node223.html"> Miscellaneous Features</A>
<B>Up:</B> <A NAME=tex2html4322 HREF="node219.html"> Errors</A>
<B> Previous:</B> <A NAME=tex2html4318 HREF="node221.html"> Specialized Error-Signaling Forms </A>
<HR> <P>
<H1><A NAME=SECTION002830000000000000000>24.3. Special Forms for Exhaustive Case Analysis</A></H1>
<P>
<A NAME=EXHAUSTIVECASEANALYSIS>The</A>
syntax for <tt>etypecase</tt> and <tt>ctypecase</tt> is the same as for
<tt>typecase</tt>, except that no <tt>otherwise</tt> clause is permitted.
Similarly, the syntax for <tt>ecase</tt> and <tt>ccase</tt> is the same as for
<tt>case</tt> except for the <tt>otherwise</tt> clause.
<P>
<tt>etypecase</tt> and <tt>ecase</tt> are similar to <tt>typecase</tt> and <tt>case</tt>,
respectively, but signal a non-continuable error rather than returning
<tt>nil</tt> if no clause is selected.
<P>
<tt>ctypecase</tt> and <tt>ccase</tt> are also similar to <tt>typecase</tt> and <tt>case</tt>,
but signal a continuable error if no clause is selected.
<P>
<BR><b>[Macro]</b><BR>
<tt>etypecase <i>keyform</i> {(<i>type</i> {<i>form</i>}*)}*</tt><P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
This control construct is similar to <tt>typecase</tt>,
but no explicit <tt>otherwise</tt> or <tt>t</tt> clause is permitted.
If no clause is satisfied, <tt>etypecase</tt> signals an error with
a message constructed from the clauses. It is not permissible to
continue from this error. To supply an application-specific error message, the
user should use <tt>typecase</tt> with an <tt>otherwise</tt> clause containing a call
to <tt>error</tt>. The name of this function stands for ``exhaustive
type case'' or ``error-checking type case.''
For example:
<P><pre>
(setq x 1/3)
(etypecase x
(integer x)
(symbol (symbol-value x)))
Error: The value of X, 1/3, is neither
an integer nor a symbol.
&gt;
</pre><P>
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1988
(CONDITION-SYSTEM) <A NAME=28167>&#160;</A>
to adopt a proposal for a Common Lisp Condition System.
This proposal modifies the definition of <tt>etypecase</tt> to specify its
interaction with the condition system.
See section <A HREF="node337.html#EXHAUSTIVECASEANALYSISCONDITIONS">29.4.3</A>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Macro]</b><BR>
<tt>ctypecase <i>keyplace</i> {(<i>type</i> {<i>form</i>}*)}*</tt><P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
This control construct is similar to <tt>typecase</tt>,
but no explicit <tt>otherwise</tt> or <tt>t</tt> clause is permitted.
The <i>keyplace</i> must be a generalized variable reference
acceptable to <tt>setf</tt>. If no clause is satisfied, <tt>ctypecase</tt> signals an
error with a message constructed from the clauses. Continuing from this
error causes <tt>ctypecase</tt> to accept a new value from the user, store
it into <i>keyplace</i>, and start over, making the type tests again.
Subforms of <i>keyplace</i> may be evaluated multiple times. The name
of this function stands for ``continuable exhaustive type case.''
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1988
(CONDITION-SYSTEM) <A NAME=28186>&#160;</A>
to adopt a proposal for a Common Lisp Condition System.
This proposal modifies the definition of <tt>ctypecase</tt> to specify its
interaction with the condition system.
See section <A HREF="node337.html#EXHAUSTIVECASEANALYSISCONDITIONS">29.4.3</A>.
<P>
X3J13 voted in March 1988 (PUSH-EVALUATION-ORDER) <A NAME=28191>&#160;</A>
to clarify order of evaluation (see section <A HREF="node80.html#SETFSECTION">7.2</A>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Macro]</b><BR>
<tt>ecase <i>keyform</i> {({({<i>key</i>}*) | <i>key</i>} {<i>form</i>}*)}*</tt><P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
This control construct is similar to <tt>case</tt>,
but no explicit <tt>otherwise</tt> or <tt>t</tt> clause is permitted.
If no clause is satisfied, <tt>ecase</tt> signals an error with a
message constructed from the clauses. It is not permissible to continue
from this error. To supply an error message, the user should use
<tt>case</tt> with an <tt>otherwise</tt> clause containing a call to <tt>error</tt>.
The name of this function stands for ``exhaustive case'' or
``error-checking case.''
For example:
<P><pre>
(setq x 1/3)
(ecase x
(alpha (foo))
(omega (bar))
((zeta phi) (baz)))
Error: The value of X, 1/3, is not
ALPHA, OMEGA, ZETA, or PHI.
</pre><P>
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1988
(CONDITION-SYSTEM) <A NAME=28210>&#160;</A>
to adopt a proposal for a Common Lisp Condition System.
This proposal modifies the definition of <tt>ecase</tt> to specify its
interaction with the condition system.
See section <A HREF="node337.html#EXHAUSTIVECASEANALYSISCONDITIONS">29.4.3</A>.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR><b>[Macro]</b><BR>
<tt>ccase <i>keyplace</i> {({({<i>key</i>}*) | <i>key</i>} {<i>form</i>}*)}*</tt><P>
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
This control construct is similar to <tt>case</tt>,
but no explicit <tt>otherwise</tt> or <tt>t</tt> clause is permitted.
The <i>keyplace</i> must be a generalized variable reference
acceptable to <tt>setf</tt>. If no clause is satisfied, <tt>ccase</tt> signals an error
with a message constructed from the clauses. Continuing from this error
causes <tt>ccase</tt> to accept a new value from the user, store it into
<i>keyplace</i>, and start over, making the clause tests again. Subforms of
<i>keyplace</i> may be evaluated multiple times. The name of this function
stands for ``continuable exhaustive case.''
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
X3J13 voted in June 1988
(CONDITION-SYSTEM) <A NAME=28230>&#160;</A>
to adopt a proposal for a Common Lisp Condition System.
This proposal modifies the definition of <tt>ccase</tt> to specify its
interaction with the condition system.
See section <A HREF="node337.html#EXHAUSTIVECASEANALYSISCONDITIONS">29.4.3</A>.
<P>
X3J13 voted in March 1988 (PUSH-EVALUATION-ORDER) <A NAME=28235>&#160;</A>
to clarify order of evaluation (see section <A HREF="node80.html#SETFSECTION">7.2</A>).
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<hr>
<b>Rationale:</b> The special forms
<tt>etypecase</tt>, <tt>ctypecase</tt>, <tt>ecase</tt>, and <tt>ccase</tt>
are included in Common Lisp, even though a user
could write them himself using the other standard facilities provided,
because it is likely that many users will want these.
Common Lisp therefore provides
a standard consistent set rather than allowing
a variety of incompatible dialects to develop.
<P>
In addition, experience has shown that
some Lisp programmers are too lazy to put an appropriate
<tt>otherwise</tt> clause into every <tt>case</tt> statement to
check for cases they
didn't anticipate, even if they would agree that it will probably
hurt them later. If an <tt>otherwise</tt> clause can be included
very easily by adding one character to the name of the construct,
it is perhaps more likely that programmers will take the trouble to do it.
<P>
The <tt>e</tt> versions do nothing more than supply
automatically generated <tt>otherwise</tt> clauses, but correct
implementation of the <tt>c</tt> versions
requires some care. It is therefore especially
important that the <tt>c</tt> versions be provided
by the system so users don't have to puzzle them out on
their own. Individual implementations may be able to do a better job
of supporting these special forms,
using their own idiosyncratic facilities, than can be done
using the error-signaling facilities defined by Common Lisp.
<hr>
<P>
<BR> <HR><A NAME=tex2html4323 HREF="node223.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html4321 HREF="node219.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html4317 HREF="node221.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html4325 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html4326 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html4324 HREF="node223.html"> Miscellaneous Features</A>
<B>Up:</B> <A NAME=tex2html4322 HREF="node219.html"> Errors</A>
<B> Previous:</B> <A NAME=tex2html4318 HREF="node221.html"> Specialized Error-Signaling Forms </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>