Node:Equality, Next:, Up:Utility Functions

24.1 Equality

Three different kinds of sameness are defined in Scheme.

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.

eq? x y Scheme Procedure
Return #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?.

eqv? x y Scheme Procedure
The 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 Scheme Procedure
Return #t iff x and y are recursively eqv? equivalent. 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.