Section 7

How to Design Programs: An Introduction to Programming and Computing

Info iconThis 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 file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-10.html (1 of 13) [2/5/2008 4:44:15 PM]
Background image of page 1

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

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming To accommodate this kind of function, Scheme provides PREDICATES, which are operations that recognize a particular form of data. The predicates for the classes of data we know are: number? , which consumes an arbitrary value and produces true if the value is a number and false otherwise; boolean? , which consumes an arbitrary value and produces true if the value is a boolean value and
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Spring '07 term at WPI.

Page1 / 13

Section 7 - How to Design Programs: An Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online