64 lines
2.3 KiB
HTML
64 lines
2.3 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> Logical Operators: and and or</TITLE>
|
||
|
<style type="text/css">
|
||
|
tt {font-weight: bold}
|
||
|
</style>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Logical Operators: And and Or">
|
||
|
<meta name="keywords" value="lp">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<A HREF="node28.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
|
||
|
<A HREF="node24.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
|
||
|
<A HREF="node26.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="node28.html"> Exercises</A>
|
||
|
<B>Up:</B>
|
||
|
<A HREF="node24.html"> More Predicates and </A>
|
||
|
<B> Previous:</B>
|
||
|
<A HREF="node26.html"> Checking for NIL</A>
|
||
|
<BR> <HR> <P>
|
||
|
<H2> Logical Operators: <tt>and</tt> and <tt>or</tt></H2>
|
||
|
<P>
|
||
|
<tt>and</tt> and <tt>or</tt> are functions but not predicates since they may return values other than t or nil. Each evaluates its arguments in the order they appear, but only enough arguments to provide a definitive result are evaluated. So, some arguments to <tt>and</tt> and to <tt>or</tt> may not be evaluated at all.
|
||
|
<P>
|
||
|
<tt>and</tt> returns nil as soon as it finds an argument which evaluates to nil; otherwise it returns the value of its last argument. For example:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (and 1 2 3 4)
|
||
|
4
|
||
|
|
||
|
> (and 1 (cons 'a '(b)) (rest '(a)) (setf y 'hello))
|
||
|
NIL
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
In the example immediately above, the expression <tt>(setf y 'hello)</tt> is never evaluated since <tt>(rest '(a))</tt> returns nil. You can check this out by evaluating y directly:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> y
|
||
|
49
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
<tt>or</tt> returns the result of its first non-nil argument, and does not evaluate the rest of its arguments. If all the arguments evaluate to nil, then <tt>or</tt> returns nil. Examples:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (or nil nil 2 (setf y 'goodbye))
|
||
|
2
|
||
|
|
||
|
> (or (rest '(a)) (equal 3 4))
|
||
|
NIL
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
Once again, you will see that y's value is unchanged by these examples.
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<P>
|
||
|
<ADDRESS>
|
||
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
||
|
March 2007 </I>
|
||
|
</ADDRESS>
|
||
|
</BODY>
|