emacs.d/clones/colinallen.dnsalias.org/lp/node25.html

129 lines
2.8 KiB
HTML
Raw Normal View History

2022-08-02 12:34:59 +02:00
<!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> Equality Predicates</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Equality Predicates">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node26.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="node24.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="node26.html"> Checking for NIL</A>
<B>Up:</B>
<A HREF="node24.html"> More Predicates and </A>
<B> Previous:</B>
<A HREF="node24.html"> More Predicates and </A>
<BR> <HR> <P>
<H2> Equality Predicates</H2>
<P>
Common Lisp contains a number of equality predicates. Here are the four most commonly used:
<P>
<DL COMPACT><DT><b>=</b>
<DD> (= x y) is true if and only x and y are numerically equal.
<P>
<DT><b>equal</b>
<DD> As a rule of thumb, (equal x y) is true if their printed representations are the same (i.e. if they look the same when printed). Strictly, x and y are equal if and only if they are structurally isomorphic, but for present purposes, the rule of thumb is sufficient.
<P>
<DT><b>eq</b>
<DD> (eq x y) is true if and only if they are the same object (in most cases, this means the same object in memory).
<P>
<DT><b>eql</b>
<DD> (eql x y) is true if and only if they are either eq or they are numbers of the same type and value.
<P>
</DL>
<P>
Generally = and equal are more widely used than eq and eql.
<P>
Here are some examples involving numbers:
<BLOCKQUOTE>
<PRE>&gt; (= 3 3.0)
T
&gt; (= 3/1 6/2)
T
&gt; (eq 3 3.0)
NIL
&gt; (eq 3 3)
T or NIL (depending on implementation of Common Lisp)
&gt; (eq 3 6/2)
T
&gt; (eq 3.0 6/2)
NIL
&gt; (eql 3.0 3/1)
NIL
&gt; (eql 3 6/2)
T
&gt; (equal 3 3)
T
&gt; (equal 3 3.0)
NIL
</PRE>
</BLOCKQUOTE>
Suppose now we have the following variable assignments:
<BLOCKQUOTE>
<PRE>&gt; (setf a '(1 2 3 4))
(1 2 3 4)
&gt; (setf b '(1 2 3 4))
(1 2 3 4)
&gt; (setf c b)
(1 2 3 4)
</PRE>
</BLOCKQUOTE>
Then:
<BLOCKQUOTE>
<PRE>&gt; (eq a b)
NIL
&gt; (eq b c)
T
&gt; (equal a b)
T
&gt; (equal b c)
T
&gt; (eql a b)
NIL
&gt; (eql b c)
T
&gt; (= (first a) (first b))
T
&gt; (eq (first a) (first b))
T or NIL (depending on implementation of Common Lisp)
&gt; (eql (first a) (first b))
T
</PRE>
</BLOCKQUOTE>
In most cases, you will want to use either = or equal, and fortunately these are the easiest to understand. Next most frequently used is eq. Eql is used by advanced programmers.
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>