next up previous
Contents Next: Checking for NIL Up: More Predicates and Previous: More Predicates and

Equality Predicates

Common Lisp contains a number of equality predicates. Here are the four most commonly used:

=
(= x y) is true if and only x and y are numerically equal.

equal
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.

eq
(eq x y) is true if and only if they are the same object (in most cases, this means the same object in memory).

eql
(eql x y) is true if and only if they are either eq or they are numbers of the same type and value.

Generally = and equal are more widely used than eq and eql.

Here are some examples involving numbers:

> (= 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

Suppose now we have the following variable assignments:
> (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)
Then:
> (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
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.



© Colin Allen & Maneesh Dhagat
March 2007