On this page:
1 Midterm advice
2 Data definitions define data
3 big-bang with structures
4 Enumeration vs structure
8.6.0.14

Lecture 9: More structures

This assignment is due on Sunday, September 18 at 11:59pm. Submit it using Handin as assignment lecture9. You only need to submit the first 6 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 Point is (make-point Number Number)
(define-struct point [x y])
 
; A Person is (make-person String Number)
(define-struct person [name age])
 
; 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 "Dan" 22) is a ???

3 big-bang with structures

Exercise 2. Design the function move-point, which takes a Point and adds 1 to both the x and the y coordinate, producing a new Point. 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 3. 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 4. Design the function half-point, which takes a Point and produces a new Point where the coordinates are half as big. Show step-by-step calculations for one of your function examples.

Exercise 5. Design the function initial, which takes a Person and produces a string which is the first letter of their name. The substring function will be helpful. Show step-by-step calculations for one of your function examples.

Exercise 6. Develop a data and structure definition for a Triple, which holds 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 the material together in class.

Exercise 7. 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 and structure definition for a Basket, which holds 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 8. 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?