6.3. Equality Predicates
Common Lisp provides a spectrum of predicates for testing for equality of two objects: eq
(the most specific), eql
, equal
, and equalp
(the most general).
eq
and equal
have the meanings traditional in Lisp.
eql
was added because it is frequently needed, and equalp
was added primarily in order to have a version of equal
that would ignore type differences when comparing numbers and case differences when comparing characters.
If two objects satisfy any one of these equality predicates, then they also satisfy all those that are more general.
.
[Function]
eq x y
(eq x y)
is true if and only if x
and y
are the same identical object. (Implementationally, x
and y
are usually eq
if and only if they address the same identical memory location.)
.
The predicate eql
is the same as eq
, except that if the arguments are characters or numbers of the same type then their values are compared. Thus eql
tells whether two objects are conceptually the same, whereas eq tells whether two objects are implementationally identical. It is for this reason that eql
, not eq
, is the default comparison predicate for the sequence functions defined in chapter 14.
.
[Function]
eql x y
The eql
predicate is true if its arguments are eq
, or if they are numbers of the same type with the same value, or if they are character objects that represent the same character.
.
[Function]
equal x y
The equal
predicate is true if its arguments are structurally similar (isomorphic) objects. A rough rule of thumb is that two objects are equal
if and only if their printed representations are the same.
Numbers and characters are compared as for eql
. Symbols are compared as for eq
. This method of comparing symbols can violate the rule of thumb for equal
and printed representations, but only in the infrequently occurring case of two distinct symbols with the same print name.
.
[Function]
equalp x y
Two objects are equalp
if they are equal
; if they are characters and satisfy char-equal
, which ignores alphabetic case and certain other attributes of characters; if they are numbers and have the same numerical value, even if they are of different types; or if they have components that are all equalp
.
— Common Lisp the Language, 2nd Edition
— Guy L. Steele Jr.
.
Conrad’s Rule of Thumb for Comparing Stuff:
1. Use eq
to compare symbols
2. Use equal
for everything else
— Land of Lisp, p.63
— Conrad Barski, M. D.
.
.
2019.01.16 Wednesday ACHK
You must be logged in to post a comment.