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 today.
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.
More formally, we might write that
; A pt_cart is a ; (make-pt_cart x y) ; where x and y are numbers.In Scheme, we can define compound data with define-struct, e.g.,
(define-struct pt_cart (x y))
|
See the solution. |
What would be different if we had instead used the following?
(define-struct pt_cart (y x)) |
We can also combine cases and structured data in interesting ways. As a simple example, consider that programs may want to use both Cartesian and polar coordinates interchangeably. Remember, polar coordinates use a radial distance from the origin, r, and an angle from the x-axis, theta. We could define these points as
; A point is either ; - (make-pt_cart x y), where x and y are numbers, or ; - (make-pt_polar r theta), where x and y are numbers.
Later in the lab we will write some programs using various kinds of points.
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:
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.
Follow the above steps to write a function that computes the distance of a pt_cart point from the origin. (Remember Pythagorus?) |
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:
|