emacs.d/clones/lisp/colinallen.dnsalias.org/lp/node87.html

94 lines
2.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> cond (MACRO)</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" cond (MACRO)">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node88.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node72.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node86.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node88.html"> cons (FUNCTION)</A>
<B>Up:</B>
<A HREF="node72.html"> Appendix: Selected Lisp </A>
<B> Previous:</B>
<A HREF="node86.html"> cdr (Function)</A>
<BR> <HR> <P>
<H1> cond (MACRO)</H1>
<P>
<b> Format:</b>
<tt> (cond (&lt;test1&gt; &lt;body1&gt; )</tt>
<tt> (&lt;test2&gt; &lt;body2&gt; )</tt>
<tt> .</tt>
<tt> .</tt>
<tt> (&lt;testN&gt; &lt;bodyN&gt; ))</tt>
<P>
<b> Required arguments:</b>
none
<P>
<b> Optional arguments:</b>
zero or more <tt> (&lt;test&gt; &lt;body&gt; )</tt> clauses
<P>
Here, <tt> &lt;test&gt; </tt> is any Lisp expression. The <tt> &lt;body&gt; </tt> is a sequence of zero or more Lisp expressions.
<P>
Each <tt> &lt;test&gt; </tt> expression is evaluated in turn, until one is
found whose value is non-NIL. For this clause, the sequence of
<tt> &lt;body&gt; </tt> expressions are evaluated and the value of the last
expression is returned as the result of cond. If no successful
<tt> &lt;test&gt; </tt> expression is found, cond returns NIL. If a successful
<tt> &lt;test&gt; </tt> expression is found, but its clause contains no <tt> &lt;body&gt; </tt>
expressions, the result of the <tt> &lt;test&gt; </tt> expression is returned as
the result of cond.
<P>
<b> Examples:</b>
<P>
<BLOCKQUOTE>
<PRE>&gt; (setq a '3)
3
&gt; (setq b '4)
4
&gt; (cond ((= a b) 'equal)
((&lt; a b) 'b-is-bigger)
(t 'a-is-bigger))
B-IS-BIGGER
&gt; (setq action 'square-both)
SQUARE-BOTH
&gt; (cond ((equal action 'clear-both)
(setq a 0)
(setq b 0))
((equal action 'square-both)
(setq a (* a a))
(setq b (* b b)))
((equal action 'square-a)
(setq a (* a a)))
((equal action 'square-b)
(setq b (* b b))))
16
&gt; (cond ((= a 5) 'found)
((= b 5) 'found))
NIL
&gt; (cond ((+ a 1))) ; strange use of cond
10 ; before this, a was 9 due to previous
; cond clauses
</PRE>
</BLOCKQUOTE>
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>