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.
Consider tax brackets as an example of a kind of data where a value fits 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 in a Cartesian coordinate system, 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 pt_cart (x y))
Q: What functions does this define? (N.B. labbies: predicates (pt_cart?) have not been mentioned in lecture. Mention them now!) Give examples of pt_carts.
A: It defines...
Thus, the corresponding data definition is as follows:
A pt_cart is a (make-pt_cart x y), where x and y are numbers.
Two selectors pt_cart-x and pt_cart-y that
can be used like
(pt_cart-x apoint2)
(pt_cart-y origin)
A selector selects the relevant piece of information from the compound thing, e.g., the "x" component of a pt_cart..
Selectors are also called destructors or accessors.
(Not mentioned in class...) One predicate pt_cart?
that can be used like
(pt_cart? origin)
(pt_cart? 3)
(pt_cart? (make-point2 3 8))
A predicate determines whether or not its one argument is of the right form, here a pt_cart. Why would we use it? We'll see...
Q: What would be different if we had instead used the following?
(define-struct pt_cart (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 this example:
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. We recommend that you always try to do all the lab exercises, even if you run out of time during lab period.
Important for all exercises:
To do:
Develop a program that computes the distance between two points in Cartesian coordinates.
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 pt_cart's and vec2's separately?
Develop a program that adds a point and a vector, returning a new point.
Be sure to follow the design recipe.