129 lines
11 KiB
HTML
129 lines
11 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Macro CASE, CCASE, ECASE</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="m_when_.htm">
|
|
<LINK REL=UP HREF="c_data_a.htm">
|
|
<LINK REL=NEXT HREF="m_tpcase.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="m_when_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_tpcase.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="case"><A NAME="ccase"><A NAME="ecase"><I>Macro</I> <B>CASE, CCASE, ECASE</B></A></A></A> <A NAME="otherwise"></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>case</B> <I>keyform {<I>normal-clause</I>}<B>*</B> [<I>otherwise-clause</I>]</I> => <I><I>result</I><B>*</B></I><P>
|
|
|
|
<B>ccase</B> <I>keyplace {<I>normal-clause</I>}<B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
|
|
|
<B>ecase</B> <I>keyform {<I>normal-clause</I>}<B>*</B></I> => <I><I>result</I><B>*</B></I><P>
|
|
<P>
|
|
<PRE>
|
|
normal-clause::= (keys form*)
|
|
</PRE>
|
|
<PRE>
|
|
otherwise-clause::= ({otherwise | t} form*)
|
|
</PRE>
|
|
<PRE>
|
|
clause::= normal-clause | otherwise-clause
|
|
</PRE>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>keyform</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated to produce a <I>test-key</I>. <P>
|
|
<I>keyplace</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; evaluated initially to produce a <I>test-key</I>. Possibly also used later as a <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> if no <I>keys</I> match. <P>
|
|
<I>test-key</I>---an object produced by evaluating <I>keyform</I> or <I>keyplace</I>. <P>
|
|
<I>keys</I>---a <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designator</I></A> for a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> of <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A>. In the case of <A REL=DEFINITION HREF="#case"><B>case</B></A>, the <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A> <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A> and <A REL=DEFINITION HREF="#otherwise"><B>otherwise</B></A> may not be used as the <I>keys</I> <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designator</I></A>. To refer to these <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A> by themselves as <I>keys</I>, the designators <TT>(t)</TT> and <TT>(otherwise)</TT>, respectively, must be used instead. <P>
|
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
|
<I>results</I>---the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> returned by the <I>forms</I> in the matching <I>clause</I>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
These <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macros</I></A> allow the conditional execution of a body of <I>forms</I> in a <I>clause</I> that is selected by matching the <I>test-key</I> on the basis of its identity. <P>
|
|
The <I>keyform</I> or <I>keyplace</I> is <I>evaluated</I> to produce the <I>test-key</I>. <P>
|
|
Each of the <I>normal-clauses</I> is then considered in turn. If the <I>test-key</I> is the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> as any <A REL=DEFINITION HREF="26_glo_k.htm#key"><I>key</I></A> for that <I>clause</I>, the <I>forms</I> in that <I>clause</I> are <I>evaluated</I> as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>, and the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> it returns are returned as the value of the <A REL=DEFINITION HREF="#case"><B>case</B></A>, <A REL=DEFINITION HREF="#ccase"><B>ccase</B></A>, or <A REL=DEFINITION HREF="#ecase"><B>ecase</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
|
|
These <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macros</I></A> differ only in their <I>behavior</I> when no <I>normal-clause</I> matches; specifically: <P>
|
|
<P><DL><P>
|
|
<DT><A REL=DEFINITION HREF="#case"><B>case</B></A> <P><DD>
|
|
If no <I>normal-clause</I> matches, and there is an <I>otherwise-clause</I>, then that <I>otherwise-clause</I> automatically matches; the <I>forms</I> in that <I>clause</I> are <I>evaluated</I> as an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>, and the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> it returns are returned as the value of the <A REL=DEFINITION HREF="#case"><B>case</B></A>. <P>
|
|
If there is no <I>otherwise-clause</I>, <A REL=DEFINITION HREF="#case"><B>case</B></A> returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<DT><A REL=DEFINITION HREF="#ccase"><B>ccase</B></A> <P><DD>
|
|
If no <I>normal-clause</I> matches, a <A REL=DEFINITION HREF="26_glo_c.htm#correctable"><I>correctable</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#error"><I>error</I></A> of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> is signaled. The offending datum is the <I>test-key</I> and the expected type is <A REL=DEFINITION HREF="26_glo_t.htm#type_equivalent"><I>type equivalent</I></A> to <TT>(member </TT><I>key1</I><TT> </TT><I>key2</I><TT> ...)</TT>. The <A REL=DEFINITION HREF="a_store_.htm#store-value"><B>store-value</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> can be used to correct the error. <P>
|
|
If the <A REL=DEFINITION HREF="a_store_.htm#store-value"><B>store-value</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> is invoked, its <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> becomes the new <I>test-key</I>, and is stored in <I>keyplace</I> as if by <TT>(setf </TT><I>keyplace</I><TT> </TT><I>test-key</I><TT>)</TT>. Then <A REL=DEFINITION HREF="#ccase"><B>ccase</B></A> starts over, considering each <I>clause</I> anew. <P>
|
|
The subforms of <I>keyplace</I> might be evaluated again if none of the cases holds. <P>
|
|
<DT><A REL=DEFINITION HREF="#ecase"><B>ecase</B></A> <P><DD>
|
|
If no <I>normal-clause</I> matches, a <A REL=DEFINITION HREF="26_glo_n.htm#non-correctable"><I>non-correctable</I></A> <A REL=DEFINITION HREF="26_glo_e.htm#error"><I>error</I></A> of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> is signaled. The offending datum is the <I>test-key</I> and the expected type is <A REL=DEFINITION HREF="26_glo_t.htm#type_equivalent"><I>type equivalent</I></A> to <TT>(member </TT><I>key1</I><TT> </TT><I>key2</I><TT> ...)</TT>. <P>
|
|
Note that in contrast with <A REL=DEFINITION HREF="#ccase"><B>ccase</B></A>, the caller of <A REL=DEFINITION HREF="#ecase"><B>ecase</B></A> may rely on the fact that <A REL=DEFINITION HREF="#ecase"><B>ecase</B></A> does not return if a <I>normal-clause</I> does not match. <P>
|
|
<P></DL><P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(dolist (k '(1 2 3 :four #\v () t 'other))
|
|
(format t "~S "
|
|
(case k ((1 2) 'clause1)
|
|
(3 'clause2)
|
|
(nil 'no-keys-so-never-seen)
|
|
((nil) 'nilslot)
|
|
((:four #\v) 'clause4)
|
|
((t) 'tslot)
|
|
(otherwise 'others))))
|
|
>> CLAUSE1 CLAUSE1 CLAUSE2 CLAUSE4 CLAUSE4 NILSLOT TSLOT OTHERS
|
|
=> NIL
|
|
(defun add-em (x) (apply #'+ (mapcar #'decode x)))
|
|
=> ADD-EM
|
|
(defun decode (x)
|
|
(ccase x
|
|
((i uno) 1)
|
|
((ii dos) 2)
|
|
((iii tres) 3)
|
|
((iv cuatro) 4)))
|
|
=> DECODE
|
|
(add-em '(uno iii)) => 4
|
|
(add-em '(uno iiii))
|
|
>> Error: The value of X, IIII, is not I, UNO, II, DOS, III,
|
|
>> TRES, IV, or CUATRO.
|
|
>> 1: Supply a value to use instead.
|
|
>> 2: Return to Lisp Toplevel.
|
|
>> Debug> :CONTINUE 1
|
|
>> Value to evaluate and use for X: 'IV
|
|
=> 5
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Side Effects:</B><P>
|
|
<P>
|
|
The debugger might be entered. If the <A REL=DEFINITION HREF="a_store_.htm#store-value"><B>store-value</B></A> <A REL=DEFINITION HREF="26_glo_r.htm#restart"><I>restart</I></A> is invoked, the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>value</I></A> of <I>keyplace</I> might be changed. <P>
|
|
<P><B>Affected By:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#ccase"><B>ccase</B></A> and <A REL=DEFINITION HREF="#ecase"><B>ecase</B></A>, since they might signal an error, are potentially affected by existing <I>handlers</I> and <A REL=DEFINITION HREF="v_debug_.htm#STdebug-ioST"><B>*debug-io*</B></A>. <P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#ccase"><B>ccase</B></A> and <A REL=DEFINITION HREF="#ecase"><B>ecase</B></A> signal an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> if no <I>normal-clause</I> matches. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_cond.htm#cond"><B>cond</B></A>, <A REL=DEFINITION HREF="m_tpcase.htm#typecase"><B>typecase</B></A>, <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A>, <A REL=CHILD HREF="05_a.htm">Section 5.1 (Generalized Reference)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(case test-key
|
|
{((key*) form*)}*)
|
|
==
|
|
(let ((#1=#:g0001 test-key))
|
|
(cond {((member #1# '(key*)) form*)}*))
|
|
</PRE>
|
|
</TT> <P>
|
|
The specific error message used by <A REL=DEFINITION HREF="#ecase"><B>ecase</B></A> and <A REL=DEFINITION HREF="#ccase"><B>ccase</B></A> can vary between implementations. In situations where control of the specific wording of the error message is important, it is better to use <A REL=DEFINITION HREF="#case"><B>case</B></A> with an <I>otherwise-clause</I> that explicitly signals an error with an appropriate message. <P>
|
|
<HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|