8.12

### Lecture 9: More structuresðŸ”—

This assignment is due on Sunday, February 4 at 11:59pm. Submit it using Handin as assignment lecture9. You only need to submit the first 8 exercises.

Our first midterm is Tuesday night! Here is some advice as you study:
• Read every word of instructions. If a problem asks you to define a constant, don’t define a function instead. If a problem tells you to design a function with a certain name, don’t use a different name. If a problem says your function should return a traffic light, don’t make it return an image.

• Define everything you use. If your signature refers to a type of data, give its data definition. If your code uses a courtesy function, give its structure definition. You don’t need to repeat a definition in the same file or on the same exam.

• Solve old problems. A great way to review is to solve old problems in lectures, labs, and problem sets. Exam problems will be similar. When you practice programming on a computer, run your program as often as you can, and try out the Stepper. When you practice programming on paper, predict what happens during testing, and work out step-by-step calculations.

Students who practice thoroughly with old problems are often surprised by how well they do on the midterm.

#### 2Data definitions define dataðŸ”—

 ; A Posn is (make-posn Number Number) ;(define-struct posn [x y]) ; A Point is (make-point Number Number) (define-struct point [x y]) (define pt1 (make-point 30 50)) (define pt2 (make-point 70 45)) ; A Person is (make-person String Number) (define-struct person [name age]) (define me (make-person "Alice" 37)) ; A TrafficLight is one of: ; - "red" ; - "yellow" ; - "green"

Exercise 1. Copy the following data into your Definitions Window. For each piece of data, say what data definition includes it. In other words, say what kind of value it is. For example, 17 is a Number, and (make-point 3 4) is a Point.
 ; Exercise 1 ; 17 is a Number ; true is a ??? ; "true" is a ??? ; "red" is a ??? ; (circle 10 "solid" "red") is a ??? ; (make-point 3 4) is a Point ; (make-person "Ken" 45) is a ??? ; (make-posn 12 34) is a ??? ; (make-point 56 78) is a ???

Exercise 2. Copy the expressions below into your Definitions Window. What are their results? Try to predict the results before asking DrRacket.
 ;   (make-person (person-name me) (+ 1 (person-age me))) ; = ??? ;   (< (point-x pt2) (point-x pt1)) ; = ??? ;   (person? me) ; = ??? ;   (person? "Alice") ; = ??? ;   (person? pt1) ; = ??? ;   (point? pt1) ; = ??? ;   (posn? pt1) ; = ???

#### 3big-bang with structuresðŸ”—

Exercise 3. Here are the four courtesy functions for the point structure we defined above. Write down their signatures.
 make-point point-x point-y point?

Exercise 4. Design the function move-point, which takes a Point and adds 1 to both the x and the y coordinate, producing a new Point.

Hint: You can watch additional videos to review how structures work and how to write a template for a function that processes a structure input.

What happens when you add [on-tick move-point] to the big-bang in the video above?

“The ontology of objects is a specifically Western construct… The object is what can be handled, manipulated, constructed, built up and broken down, with clear accountability of matter gained and lost.” —Iris Marion Young

Exercise 5. For the move-point function, what’s wrong with each of the following?
1.  ; A Point is Number Number
2.  ; move-point : Number Number -> Number Number
3.  (define (move-point x y) ...)
4.  ; given: 10 20   expect: 11 21
5.  (define (move-point p) (+ 1 (point-x p)) (+ 1 (point-y p)))

Exercise 6. What are the four courtesy functions for the person structure we defined above? Write down their signatures.

Exercise 7. Design a function called teenager? that takes a Person and determines whether the age is between 13 and 19 (inclusive).

Exercise 8. Develop a data definition for a Triple and a structure definition for a triple to hold three numbers. Design a function average-triple which takes a Triple and produces the average of its three numbers. Then design a function normalize-triple which takes a Triple and subtracts the average of its three numbers from each of its three numbers, producing a new Triple. For example, normalizing the triple 5 2 2 should produce the triple 2 −1 −1.

For the rest of this page, you don’t need to submit anything, but do try the exercises anyway. We’ll work through similar material together in class.

Exercise 9. Suppose that someone already wrote a data definition for Fruit and designed the following function:
 ; rate-fruit : Fruit -> Number ; rate how delicious the given fruit is (define (rate-fruit f) ...)
Develop a data definition for a Basket and a structure definition for a basket to hold three Fruits. Design a function rate-basket that takes a Basket as input and returns a number that is the average of the ratings produced by the rate-fruit function for the three Fruits in the given Basket.

#### 4Enumeration vs structureðŸ”—

It’s easy to confuse an enumeration and a structure. Below is a side-by-side comparison.

Enumeration

Structure

Data definition

 ; A Dessert is one of: ; - "chocolate" ; - "tofu" ; - "affogato"

Structure definition

None

Courtesy functions

None

Processing template

 (define (process-dessert d) (cond [(string=? d "chocolate") ...] [(string=? d "tofu") ...] [(string=? d "affogato") ...]))

Function signature

 ; draw-dessert : Dessert -> Image

Function examples

 (check-expect (draw-dessert "chocolate") ) (check-expect (draw-dessert "tofu") ) (check-expect (draw-dessert "affogato") )

Function template

 (define (draw-dessert d) (cond [(string=? d "chocolate") ...] [(string=? d "tofu") ...] [(string=? d "affogato") ...]))