Three different kinds of sameness are defined in Scheme.
- Two values can refer to exactly the same object.
- Two objects can have the same value.
- Two objects can be structurally equivalent.
The differentiation between these three kinds is important, because
determining whether two values are the same objects is very efficient,
while determining structural equivalence can be quite expensive
(consider comparing two very long lists). Therefore, three different
procedures for testing for equality are provided, which correspond to
the three kinds of sameness defined above.
#t iff x references the same object as y.
eq? is similar to
eqv? except that in some cases it is
capable of discerning distinctions finer than those detectable by
|eqv? x y
eqv? procedure defines a useful equivalence relation on objects.
Briefly, it returns
#t if x and y should normally be
regarded as the same object. This relation is left slightly open to
interpretation, but works for comparing immediate integers, characters,
and inexact numbers.
|equal? x y
#t iff x and y are recursively
equal? recursively compares the contents of pairs,
vectors, and strings, applying
eqv? on other objects such as
numbers and symbols. A rule of thumb is that objects are generally
equal? if they print the same.
equal? may fail to
terminate if its arguments are circular data structures.