There are three kinds of core equality predicates in Scheme, described
below. The same kinds of comparisons arise in other functions, like
memq
and friends (see List Searching).
For all three tests, objects of different types are never equal. So
for instance a list and a vector are not equal?
, even if their
contents are the same. Exact and inexact numbers are considered
different types too, and are hence not equal even if their values are
the same.
eq?
tests just for the same object (essentially a pointer
comparison). This is fast, and can be used when searching for a
particular object, or when working with symbols or keywords (which are
always unique objects).
eqv?
extends eq?
to look at the value of numbers and
characters. It can for instance be used somewhat like =
(see Comparison Predicates) but without an error if one operand isn’t a
number.
equal?
goes further, it looks (recursively) into the contents
of lists, vectors, etc. This is good for instance on lists that have
been read or calculated in various places and are the same, just not
made up of the same pairs. Such lists look the same (when printed),
and equal?
will consider them the same.
The Scheme procedure returns #t
if all of its arguments are the
same object, except for numbers and characters. The C function does the
same but takes exactly two arguments. For example,
(define x (vector 1 2 3)) (define y (vector 1 2 3)) (eq? x x) ⇒ #t (eq? x y) ⇒ #f
Numbers and characters are not equal to any other object, but the
problem is they’re not necessarily eq?
to themselves either.
This is even so when the number comes directly from a variable,
(let ((n (+ 2 3))) (eq? n n)) ⇒ *unspecified*
Generally eqv?
below should be used when comparing numbers or
characters. =
(see Comparison Predicates) or char=?
(see Characters) can be used too.
It’s worth noting that end-of-list ()
, #t
, #f
, a
symbol of a given name, and a keyword of a given name, are unique
objects. There’s just one of each, so for instance no matter how
()
arises in a program, it’s the same object and can be
compared with eq?
,
(define x (cdr '(123))) (define y (cdr '(456))) (eq? x y) ⇒ #t (define x (string->symbol "foo")) (eq? x 'foo) ⇒ #t
int
scm_is_eq (SCM x, SCM y)
¶Return 1
when x and y are equal in the sense of
eq?
, otherwise return 0
.
The ==
operator should not be used on SCM
values, an
SCM
is a C type which cannot necessarily be compared using
==
(see The SCM Type).
The Scheme procedure returns #t
if all of its arguments are the
same object, or for characters and numbers the same value. The C function
is similar but takes exactly two arguments.
On objects except characters and numbers, eqv?
is the same as
eq?
above. (eqv? x y)
is true if x and y are
the same object.
If x and y are numbers or characters, eqv?
compares
their type and value. An exact number is not eqv?
to an
inexact number (even if their value is the same).
(eqv? 3 (+ 1 2)) ⇒ #t (eqv? 1 1.0) ⇒ #f
The Scheme procedure returns #t
if all of its arguments are the
same type, and their contents or value are equal. The C function is
similar, but takes exactly two arguments.
For a pair, string, vector, array or structure, equal?
compares the
contents, and does so using the same equal?
recursively,
so a deep structure can be traversed.
(equal? (list 1 2 3) (list 1 2 3)) ⇒ #t (equal? (list 1 2 3) (vector 1 2 3)) ⇒ #f
For other objects, equal?
compares as per eqv?
above,
which means characters and numbers are compared by type and value (and
like eqv?
, exact and inexact numbers are not equal?
,
even if their value is the same).
(equal? 3 (+ 1 2)) ⇒ #t (equal? 1 1.0) ⇒ #f
Hash tables are currently only compared as per eq?
, so two
different tables are not equal?
, even if their contents are the
same.
equal?
does not support circular data structures, it may go
into an infinite loop if asked to compare two circular lists or
similar.
GOOPS object types (see GOOPS), including foreign object types
(see Defining New Foreign Object Types), can have an equal?
implementation specialized on two values of the same type. If
equal?
is called on two GOOPS objects of the same type,
equal?
will dispatch out to a generic function. This lets an
application traverse the contents or control what is considered
equal?
for two objects of such a type. If there’s no such
handler, the default is to just compare as per eq?
.