Index: Cases, Structured data, Design recipe, Exercises
In class, we've now seen two ways of structuring data, but have not gone through all steps of the design recipe for them. Let's do so.
Last week, we looked at tax brackets as an example of a kind of data where a value fit into one of a few cases. This week, we stated that we should always write a "data analysis" of our data. Let's see how these two fit together in this example:
; A taxable-wage is one of ; - a number from 0 inclusive to 50 exclusive, ; - a number from 50 inclusive to 150 exclusive, or ; - a number equal or greater than 150.
This "x, y, or z" structure is very common, and we will take advantage of it in our design recipe.
In class, we've introduced the idea of structured or compound data. It uses a "x, y, and z" structure which is also very common. Let's quickly review it with another example.
How would you represent a point (in two-dimensional space)? High-school algebra and geometry tells us that a point consists of an x-coordinate and a y-coordinate, where each coordinate is a number. So, we would like to create a piece of data which is a pair of numbers. In addition to creating points, we also want to be able to look at the coordinates in a point, i.e., we need to be able to take a point apart again.
In Scheme, we can define compound data with define-struct, e.g.,
(define-struct point2 (x y))Q: What functions does this define? Give examples.
A: It defines...
One constructor make-point2 that can be used like
(define origin (make-point2 0 0)) (define apoint2 (make-point2 5 7))
A constructor constructs one of the indicated compound things, here a point2, when given as many arguments as needed, here two.
Thus, the corresponding data definition is as follows:
A point2 is a (make-point2 x y), where
x and y are numbers.
Two selectors point2-x and point2-y that can be used like
(point2-x apoint2) (point2-y origin)
A selector selects the relevant piece of information from the compound thing, e.g., the "x" component of a point2.
Selectors are also called destructors or accessors.
(Not mentioned in class...) One predicate point2? that can be used like
(point2? origin) (point2? 3) (point2? (make-point2 3 8))
A predicate determines whether or not its one argument is of the right form, here a point2. Why would we use it? We'll see...
Q: What would be different if we had instead used the following?
(define-struct point2 (y x))
We will write some programs using points in just a minute.
We know our programs should take advantage of the structure of the data. Now that we know about two additional kinds of data that have more structure than arbitrary numbers:
To do: Follow the following steps for two examples:
And here's our newly expanded design recipe. (Note to labbies: Students haven't seen all these steps in class.)
Note: The template serves as a reminder to us of what the function probably looks like. We aren't obligated to use all or any of the selectors when writing a particular function.
As in many labs, there are more exercises here than most students can do in one lab section. Do at least a representative sample of the exercises in lab and/or at home. We will continue with many other examples like these in lab, class, and homework.
Important for all exercises:
To do:
Develop a program that computes the distance between two points.
Define a structure of vectors (in two-dimensional space):
(define-struct vec2 ...)
Q: A vector is also a pair of numbers, so why would we define point2s and vec2s separately?
Develop a program that adds a point and a vector, returning a new point. Be sure to follow the design recipe.