8.6.0.14
Lecture 8: Structures
This assignment is due on Tuesday, September 13 at 11:59pm. Submit it using Handin as assignment lecture8.
Remember to follow the design recipe whenever you design or write a function. In particular, every type mentioned in a signature must be introduced by a data definition, except for these well-known types: Number, Image, String, Color, Boolean.
1 Defining structures
Exercise 1.
Copy the definitions below into your Definitions Window. Make sure you know
which lines are data definitions, which lines are structure definitions, and
which lines are constant definitions.
; A Point is (make-point Number Number) (define-struct point [x y]) (define here (make-point 30 50)) (define there (make-point 70 45)) ; A Person is (make-person String Number) (define-struct person [name age]) (define me (make-person "Alice" 37)) (define you (make-person "Bob" 22))
Then, copy the expressions below into your Definitions Window.
What are their results? Try to predict the results before asking DrRacket.
(If it runs into an error, write “error” after the last step.)
; Exercise 1 ; (point-x there) ; = (point-x (make-point 70 45)) ; = ??? ; (point-y here) ; = ??? ; = ... ; (person-name you) ; = ??? ; = ... ; (point-x me) ; = ??? ; = ... ; (make-person "Carol" 21) ; = ???
Show step-by-step calculations by adding comments.
Use the Stepper to confirm that your steps are correct.
Exercise 2.
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 3. What are the four courtesy functions for the person structure we defined above? Write down their signatures.
Exercise 4.
Copy the expressions below into your Definitions Window.
What are their results? Try to predict the results before asking DrRacket.
; (person? me) ; = ??? ; = ... ; (person? "Alice") ; = ??? ; (person? there) ; = ???
Show step-by-step calculations by adding comments.
Use the Stepper to confirm that your steps are correct.
Exercise 5.
Copy the expressions below into your Definitions Window.
What are their results? Try to predict the results before asking DrRacket.
(If it runs into an error, write “error” after the last step.)
; (- (point-x there) (point-x here)) ; = ??? ; = ... ; (< (person-age me) (person-age you)) ; = ??? ; = ... ; (point-x (point-x there)) ; = ??? ; = ... ; (person-age (make-person "Carol" 21)) ; = ??? ; (make-person (person-name me) (+ 1 (person-age me))) ; = ???
Show step-by-step calculations by adding comments.
Use the Stepper to confirm that your steps are correct.
2 Designing functions using structures
Exercise 6.
Finish designing the following function.
; 1. Data Definitions ; A Person is (make-person String Number) (define-struct person [name age]) ; A Boolean is one of: ; - true ; - false ; 2. Signature, Purpose, Header ; teenager? : Person -> Boolean ; is a given person 13-19 years old? (define (teenager? p) ...) ; 3. Examples ; 4. Template ; 5. Definition ; 6. Tests
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.
Optional: Read Chapter 5 of the textbook.