How to Design Programs: An Introduction to Programming and Computing

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 7 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: 24 numbers: representations of numeric information; booleans: truth and falsity; symbols: representations of symbolic information; and structures: 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. 7.1 Mixing and Distinguishing Data In the preceding section, we used posn structures with exactly two components to represent pixels. If many of the pixels are on the x axis, we can simplify the representation by using plain numbers for those pixels and posn structures for the remaining ones. Figure 16 contains a sample collection of such points. Three of the five points, namely, C , D , and E , are on the x axis. Only two points require two coordinates for an accurate description: A and B . Our new idea for representing points permits us to describe this class of points succinctly: (make-posn 6 6) for A ; (make-posn 1 2) for B ; and 1, 2, and 3 for C , D , and E , respectively. If we now wish to define the function distance-to-0 , 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 or a posn . Depending on the class to which the input belongs, distance-to-0 must employ a different method to calculate the distance to the origin. Thus we need to use a cond -expression to distinguish the two cases. Unfortunately, we don't have any operations to formulate the appropriate conditions. [curriculum1ab-Z-G-1.gif] Figure 16: A small collection of points (1 of 13) [2/5/2008 4:44:15 PM]
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon