Comp 210 Lab 2: Simple data structures

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.


Cases

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.


Structured data

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)) 

Working with Cartesian points
  1. What functions are defined by this use of define-struct?
  2. Give examples of pt_carts.
See the solution.

Question
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.


Design Recipe

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:

Let's add to our design recipe to use these kinds of structure.

And here's our newly expanded design recipe. (Note to labbies: Students haven't seen all these steps in class.)

  1. Define your data.
  2. Make examples of the data.
  3. Write the function's contract, purpose, and header.
  4. Make examples of the function's use.
  5. Make a template for the function body. The template should remind you how to take advantage of the structure in the data definition.
    1. Use a cond expression with the same number of clauses as the data definition has cases. Use the appropriate predicates in tests for each case. (If the data definition has only one case, skip the cond.) In particular, consider the situation where you must differentiate between different data types.
    2. In each case, show the uses of selectors.

    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.

  6. Write the function body.
  7. Test the function.

Using the design recipe
Follow the above steps to write a function that computes the distance of a pt_cart point from the origin. (Remember Pythagorus?)


Exercises

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:

Exercises
  1. Develop a program that computes the distance between two points in Cartesian coordinates.

  2. 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?

  3. Develop a program that adds a Cartesian point and a vector, returning a new Cartesian point. Be sure to follow the design recipe.

  4. Write a function that can take a Cartesian point or a polar point and return the distance of that point from the origin.

  5. Write a function that can calculate the distance between two points, given any combinagtion of Cartesian or polar coordinates (i.e., one of each type or both the same type). Hint: Wouldn't it be easiest if both were in Cartesian coordinates? Ask for help if you don't remember your trigonometry.