The Grinnell Scheme Web: The equal?
procedure

OK, then, how would you find out whether two pairs have the same components, if eq? cam't be relied on to do it?

You'd use the equal? procedure instead:

> (equal? (cons 1 2) (cons 1 2))
#t
When it's given two data structures of the same type, the equal? procedure takes them apart and checks whether the components are all equal.

What if the components are also pairs or other data structures?

Then it takes those apart, too. The equal? procedure does as much analysis as it takes to get down to unstructured components:

> (equal? (cons (cons 1 2) (cons 3 4)) (cons (cons 1 2) (cons 3 4)))
#t
> (equal? (cons (cons 1 2) (cons 3 4)) (cons (cons 1 2) (cons 3 119)))
#f
Does it give the same answer as eq? when applied to unstructured values?

Not always. Like eq?, equal? always returns the false Boolean value if its operands are of different data types, and it always returns the same value as eq? if the operands are Booleans, symbols, or the empty list. Unlike eq?, the equal? procedure is guaranteed to return the true Boolean value if each of its operands is the same character value, even if it is computed in different ways. Similarly, if each of the operands is the same number, equal? will recognize this fact and return the true Boolean value, while eq? may return the false Boolean value the operands, though numerically equal, have different computational histories.


Next topic
Previous topic
Table of contents


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/scheme-web/equal-ques.html


created August 6, 1995
last revised December 27, 1995

Copyright 1995 by John David Stone (stone@math.grin.edu)