On this page:
1 Defining structures
2 Designing functions using structures
8.14

Lecture 8: Structures🔗

This assignment is due on Tuesday, September 17 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, Posn.

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.
; An Address is (make-address Number String)
(define-struct address [number street])
(define here  (make-address 30 "Main St"))
(define there (make-address 70 "Penny Ln"))
 
; 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
 
;   (address-street there)
; = (address-street (make-address 70 "Penny Ln"))
; = ???
 
;   (address-number here)
; = ???
; = ...
 
;   (person-name you)
; = ???
; = ...
 
;   (address-street 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 address structure we defined above. Write down their signatures.
make-address
address-street
address-number
address?

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.)
;   (- (address-number there) (address-number here))
; = ???
; = ...
 
;   (< (person-age me) (person-age you))
; = ???
; = ...
 
;   (address-number (address-number 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

Optional: Read Chapter 5 of the textbook.