# Lab 4: Structures (redux)

Lab 3 notes

## Structure Definitions and Computation

Here is a structure definition:
`(define-struct bazzle (sniz bop))`
What's a bazzle? Beats me.

Exercise 1 (lightning speed): What function names are introduced by the above structure definition for `bazzle`?

Here are some Beginning Student expressions. Let's send them through the stepper in DrScheme.

```(bazzle-sniz (make-bazzle 'one 2))

(bazzle-bop
(make-bazzle (+ 1 (string-length "ar"))
(string-append "fo" "ur")))
```

The above is all code; It shows us how things evaluate. But its not a real program, because these bazzles are meaningless structures; we don't have a data definition describing what they are! We haven't designed bazzle at all!

## Data Definitions for Structured Data

Let's design some programs that we'll use in a hi-tech restaurant.
```(define-struct plate (radius color temp))
;; A Plate is a (make-plate Number Color Number)

;; interpretation: (make-plate n c t) represents a plate with radius n
;; inches, heated to t degrees farenheit, and colored c.```

Is this Data Definition okay? ... no, because we don't know what a Color is.

```;; A Color is one of
;; - 'red
;; - 'blue
;; - 'green
;; - 'black
;; - 'white```

Last, we need some example plates:

```(define frozen-dot (make-plate 1 'black 0))
(define small-red-plate (make-plate 6 'red 60))
(define large-blue-plate (make-plate 12 'blue 200))```

Exercise 2 (lightning speed): What function names are introduced by the above structure definition? What are the contracts for each introduced function? (Hint: the contracts are implied by the Data Definition for Plate.)

Exercise 3: What is the template for a function that processes a single plate?

Exercise 4: Design a function, `plate-hot-enough?`, that will tell us if a plate has reached a particular temperature, (e.g. so that we know whether its ready to hold food straight from the oven, or if its too hot to hold without something to protect your hands).

Exercise 5: Consider the following test:
`(equal? (plate-hot-enough? large-blue-plate (+ 500 500)) false)`
When does the Beginning Student language determine what `(+ 500 500)` is in the above test expression? Does it do it before or after it has extracted the plate temperature from the plate? First make a hypothesis about which comes first, then send the last test above through the stepper to find out whether your hypothesis was correct.

## More Structures

The restaurant doesn't only serve food on plates, of course. It needs to put some foods into bowls. Bowls have a radius, a color and a temperature (just like plates), but they also have a depth.

```(define-struct bowl (radius color temp depth))
;; A Bowl is a (make-bowl Number Color Number Number)

;; interpretation: (make-bowl n c t d) represents a bowl with radius n
;; inches and depth d inches, heated to t degrees farenheit, and
;; colored c.

;; Example bowls:
(define small-green-bowl (make-bowl 5 'green 60 4))
(define large-red-bowl (make-bowl 10 'red 60 5))
(define large-blue-bowl (make-bowl 10 'blue 150 6))```

Exercise 6: What is the template for a function that consumes a plate and a bowl?

Our restaurant, like any nice restaurant, always puts bowls on top of plates before handing it to the customer. Obviously you can't put too large a bowl onto a small plate; it won't fit! What's more, the owner wants to ensure that we always match the two colors of the bowl and the plate.

Exercise 7a: Design a function, `bowl-compatible?`, that consumes a bowl and a plate, and determines whether the bowl can be served atop the plate. Make sure you have enough examples/tests to cover all of the cases!

Exercise 7b: In your version of `bowl-compatible?`, which happens first: the symbol comparison "question", or the number comparison "question"? Make a hypothesis, then send some tests through the stepper to test your hypothesis.

## Unions of Structures

So we have plates and bowls. Another kind of dish is an rectangular metal tray.
```(define-struct tray (length width temp))
;; A Tray is a (make-tray Number Number Number)

;; interpretation: (make-tray l w t) is a black tray l
;; inches long, w inches wide, and heated to t degrees farenheit```

So there are lots of kinds of dishes floating around in our restaurant. Sometimes we care about what kind of dish we have in our hands. Other times we don't want to think about what kind of dish we have. So we define a class of data representing all dishes.

```;; A Dish is one of
;; - Plate
;; - Bowl
;; - Tray```

Exercise 8: What is the template for a function that processes `Dish`es?

Exercise 9: Design a function that consumes a dish and a temperature t, and produces a dish that is exactly the same as the input dish except that new dish has been heated to t degrees farenheit.

Exercise 10: Design a function that determines the capacity of a dish; that is, how much food it holds (in terms of cubic inches).

The restaurant domain expert says: you can assume that the bowls have volume: 1/2*4/3*pi*r^3*d/r = 2/3*pi*d*r^2, where d is depth. You can also assume that plates and trays each can hold food layered up to two inches high; thus a plate has volume 2*pi*r^2 and a tray has volume 2*l*w.

## Unions of Mixed-Data

The restaurant also has cups. All of the cups in the restaurant have the same radius (2in); only their heights vary. Therefore, we do not need a full-fledged structure to represent our cups.
```;; A Container is one of:
;; - (make-bowl Number Color Number Number)
;; - Number

;; interpretation:
;; A (make-bowl r c t d) is a bowl of radius r inches, depth d inches,
;; color c, and temperature t degrees farenheit.
;; A number n is a cylindrical cup of radius 2 inches and height n.```

Exercise 11: Design a function that returns the height of a `Container`.

Exercise 12: Design a function that consumes a `Container`, and produces a similar `Container` that is twice as tall.

## Lists

Now our restaurant says that they're ready to tackle programming the reservation system.

When a party calls in to make a reservation, we need to update the reservation book and add the name of the party. So we need some sort of data which can hold multiple things; sounds like a structure...

`(define-struct reservation-book (first-party second-party third-party ...))`
But... we don't know ahead of time how many reservations we might have to make!

So instead, we need to think of another way to represent this information.

Hmmmmmmmmmmmmmmmmmmmmm ..........