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
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 ; 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
“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 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.
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"