129 lines
2.8 KiB
HTML
129 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> 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>> (= 3 3.0)
|
||
|
T
|
||
|
|
||
|
> (= 3/1 6/2)
|
||
|
T
|
||
|
|
||
|
> (eq 3 3.0)
|
||
|
NIL
|
||
|
|
||
|
> (eq 3 3)
|
||
|
T or NIL (depending on implementation of Common Lisp)
|
||
|
|
||
|
> (eq 3 6/2)
|
||
|
T
|
||
|
|
||
|
> (eq 3.0 6/2)
|
||
|
NIL
|
||
|
|
||
|
> (eql 3.0 3/1)
|
||
|
NIL
|
||
|
|
||
|
> (eql 3 6/2)
|
||
|
T
|
||
|
|
||
|
> (equal 3 3)
|
||
|
T
|
||
|
|
||
|
> (equal 3 3.0)
|
||
|
NIL
|
||
|
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
Suppose now we have the following variable assignments:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (setf a '(1 2 3 4))
|
||
|
(1 2 3 4)
|
||
|
|
||
|
> (setf b '(1 2 3 4))
|
||
|
(1 2 3 4)
|
||
|
|
||
|
> (setf c b)
|
||
|
(1 2 3 4)
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
Then:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (eq a b)
|
||
|
NIL
|
||
|
|
||
|
> (eq b c)
|
||
|
T
|
||
|
|
||
|
> (equal a b)
|
||
|
T
|
||
|
|
||
|
> (equal b c)
|
||
|
T
|
||
|
|
||
|
> (eql a b)
|
||
|
NIL
|
||
|
|
||
|
> (eql b c)
|
||
|
T
|
||
|
|
||
|
> (= (first a) (first b))
|
||
|
T
|
||
|
|
||
|
> (eq (first a) (first b))
|
||
|
T or NIL (depending on implementation of Common Lisp)
|
||
|
|
||
|
> (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>© Colin Allen & Maneesh Dhagat <BR>
|
||
|
March 2007 </I>
|
||
|
</ADDRESS>
|
||
|
</BODY>
|