This preview shows pages 1–3. Sign up to view the full content.
How to Design Programs: An Introduction to Computing and Programming
[Go to
first
,
previous
,
next
page;
contents
;
index
]
Section 42
Equality
As we mutate structures or vectors, we use words such as ``the vector now contains
false
in its first
field'' to describe what happens. Behind those words is the idea that the vector itself stays the same 
even though its properties change. What this observation suggests is that there are really two notions of
equality: the one we have used so far and a new one based on effects on a structure or vector.
Understanding these two notions of equality is critically important for a programmer. We therefore
discuss them in detail in the following two subsections.
42.1
Extensional Equality
Recall the class of
posn
structures from part
I
. A
posn
combines two numbers; its fields are called
x
and
y
. Here are two examples:
(makeposn 3 4) (makeposn 8 6)
They are obviously distinct. In contrast, the following two
(makeposn 12 1) (makeposn 12 1)
are equal. They both contain
12
in the
x
field and
1
in the
y
field.
More generally, we consider two structures to be equal if they contain equal components. This assumes
that we know how to compare the components, but that's not surprising. It just reminds us that
processing structures follows the data definition that comes with the structure definition. Philosophers
refer to this notion of equality as EXTENSIONAL EQUALITY.
Section
17.8
introduced extensional equality and discussed its use for building tests. As a reminder, let's
consider a function for determining the extensional equality of
posn
structures:
;; equalposn : posn posn
>
boolean
;; to determine whether two posns are extensionally equal
(define (equalposn p1 p2)
file:///C/Documents%20and%20Settings/Linda%20Grauer.
../How%20to%20Design%20Programs/curriculumZH52.html (1 of 6) [2/5/2008 4:59:50 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentHow to Design Programs: An Introduction to Computing and Programming
(and (= (posnx p1) (posnx p2))
(= (posny p1) (posny p2))))
The function consumes two
posn
structures, extracts their field values, and then compares the
corresponding field values using
=
, the predicate for comparing numbers. Its organization matches that
of the data definition for
posn
structures; its design is standard. This implies that for recursive classes
of data, we naturally need recursive equality functions.
Exercise 42.1.1.
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '07
 Fisler

Click to edit the document details