How to Design Programs: An Introduction to Computing and Programming
The Varieties of Data
The previous section significantly expands our world of data. We must now deal with a universe that contains
booleans, symbols, and structures of many kinds. Let's bring some order to this world.
Up to this point, our functions have always processed subclasses of four different kinds of data:
representations of numeric information;
truth and falsity;
representations of symbolic information; and
representations of compounds of information.
On occasion, however, a function must process a class of data that includes both numbers and structures or structures of
several different kinds. We learn to design such functions in this section. In addition, we learn how to protect functions
from bad uses. Here a bad use means that some user can accidentally apply a function for drawing circles to a
rectangle. Although we have agreed that such users violate our data definitions, we should nevertheless know how to
protect our functions against such uses, when necessary.
Mixing and Distinguishing Data
In the preceding section, we used
structures with exactly two components to represent pixels. If many of the pixels
are on the
axis, we can simplify the representation by using plain numbers for those pixels and
the remaining ones.
contains a sample collection of such points. Three of the five points, namely,
, are on the
two points require two coordinates for an accurate description:
. Our new idea for representing points permits us
to describe this class of points succinctly:
(make-posn 6 6)
(make-posn 1 2)
; and 1, 2, and 3 for
If we now wish to define the function
, which consumes such point representations and produces
their distance to the origin, we are confronted with a problem. The function may be applied to a number
Depending on the class to which the input belongs,
must employ a different method to calculate
the distance to the origin. Thus we need to use a
-expression to distinguish the two cases. Unfortunately, we don't
have any operations to formulate the appropriate conditions.
A small collection of points
..How%20to%20Design%20Programs/curriculum-Z-H-10.html (1 of 13) [2/5/2008 4:44:15 PM]