Lecture 9: More structures
This assignment is due on Sunday, September 22 at 11:59pm. Submit it using Handin as assignment lecture9. You only need to submit the first 8 exercises.
1 Midterm advice
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 ; 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 ???
; (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) ; = ???
3 big-bang with structures
make-point point-x point-y 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 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.
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 |
|
| |||||||
Structure definition | None |
| |||||||
Courtesy functions | None |
| |||||||
Processing template |
|
| |||||||
Function signature |
|
| |||||||
Function examples |
|
| |||||||
Function template |
|
| |||||||
Function definition |
|
|
; 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"