On this page:
1 Midterm advice
2 Data definitions define data
3 big-bang with structures
4 Enumeration vs structure
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.

1 Midterm advice🔗

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.

2 Data 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)
; = ???
Show step-by-step calculations by adding comments. Use the Stepper to confirm that your steps are correct.

3 big-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.

4 Enumeration 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"
; A Graduate is (make-grad String Number)

Structure definition

None

(define-struct grad (name year))

Courtesy functions

None

; make-grad : String Number -> Graduate
; grad-name : Graduate -> String
; grad-year : Graduate -> Number
; grad? : Anything -> Boolean

Processing template

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

Function signature

; draw-dessert : Dessert -> Image
; draw-graduate : Graduate -> Image

Function examples

(check-expect (draw-dessert "chocolate") )
(check-expect (draw-dessert "tofu") )
(check-expect (draw-dessert "affogato") )
(check-expect (draw-graduate (make-grad "Ken" 1999)) )
(check-expect (draw-graduate (make-grad "Sam" 2003)) )

Function template

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

Function definition

(define (draw-dessert d)
  (cond [(string=? d "chocolate") ]
        [(string=? d "tofu") ]
        [(string=? d "affogato") ]))
(define (draw-grad g)
  (overlay (text (number->string (grad-year g)) 10 "black")
           ))

Exercise 10. Below is a complete set of data definitions. Which data definitions are enumerations? Which data definitions are structures?
; A PumpkinPie is (make-pie Crust Filling ToastedPecans)
(define-struct pie [crust filling toasted-pecans])
 
; A Crust is (make-crust Dough Boolean)
(define-struct crust [dough egg-wash])
 
; A Dough is one of:
; - "Claire's rye"
; - "Brad's AP flour"
 
; A Filling is one of:
; - "Libby"
; - "Brad, Claire and Libby"
 
; A ToastedPecans is one of:
; - "butter + sugar"
; - "sugar + egg whites"
Define some examples of PumpkinPies. Then, design some functions that process PumpkinPies. What goes wrong if we change Crust to an enumeration or change ToastedPecans to a structure?