Lab 4: Structures
Note: Whenever you design or write a function, you need to follow the design recipe.
;;; 1. Data definition ; A Department is one of: ; - "biology" ; - "business" ; - "computer science" ; - "English" ;;; 2. Signature, Purpose, Header ; salary : Department -> Number ; returns the salary for each department. ; (define (salary d) ...) ;;; 3. Examples (check-expect (salary "biology") 100000) (check-expect (salary "business") 110000) (check-expect (salary "computer science") 120000) (check-expect (salary "English") 130000) ;;; 4. Template (define (process-department d) (cond [(string=? d "biology") ...] [(string=? d "business") ...] [(string=? d "computer science") ...] [(string=? d "English") ...])) ;;; 5. Definition (define (salary d) (cond [(string=? d "biology") 100000] [(string=? d "business") 110000] [(string=? d "computer science") 120000] [(string=? d "English") 130000])) ;;; 6. Testing ; All 4 tests passed!
1 Data definitions of structures
This week we have studied a new kind of data definition: structures. A structure packages several values into a single new value. The built-in posn structure packages x and y values into a single new value. But we also define our own structures.
Consider the following vehicle structure, which contains three fields: company, model, and year.
; A Vehicle is (make-vehicle String String Number) (define-struct vehicle (company model year))
Exercise 1. Which line above is a data definition? Which line above is a structure definition? Which line above has the types of each field? Which line above has the names of each field? Don’t get these backwards!
Exercise 2. Put the data definition above into the Definitions Window of DrRacket. What happens when you run the program? What happens if you remove the semicolon then run the program?
The data definition on the first line is a comment, so the Beginning Student language does not enforce the types of the fields. But the structure definition on the second line is not a comment, so the Beginning Student language does enforce how many fields and what they are called.
Exercise 3. A book has a title, author, and number of pages. Develop a data and structure definition for a book.
Exercise 4. An instructor has a name, department, and salary. Develop a data and structure definition for an instructor. Does your data definition for an instructor refer to the data definition for a department? It should.
2 Examples of structures
With the data definition of a Vehicle in hand, we can define the following three examples of Vehicles:
(define brians-vehicle (make-vehicle "Toyota" "Camry" 2005)) (define marks-vehicle (make-vehicle "Ford" "F-150 Flareside" 1998)) (define rachaels-vehicle (make-vehicle "Chrysler" "PT Cruiser" 2005))
In the Interactions Window, we can retrieve the examples of Vehicles that we defined:
> brians-vehicle |
(make-vehicle "Toyota" "Camry" 2005) |
> marks-vehicle |
(make-vehicle "Ford" "F-150 Flareside" 1998) |
> rachaels-vehicle |
(make-vehicle "Chrysler" "PT Cruiser" 2005) |
A department is only one of the values: biology, business, computer science, or English. But a vehicle is all of the fields: company, model, and year.
Exercise 5. Is the following an example of a Vehicle? What happens when DrRacket runs this definition? Why?
(define kens-vehicle (make-vehicle "Bicycle" 1982))
Exercise 6. Is the following an example of a Vehicle? What happens when DrRacket runs this definition? Why?
(define elons-vehicle (make-vehicle 2018 "SpaceX" "Falcon-9 Heavy"))
Exercise 7. Define two examples of books.
Exercise 8. Define two examples of instructors.
3 Courtesy functions for structures
; 1. make-vehicle : String String Number -> Vehicle (constructor) ; 2. vehicle-company : Vehicle -> String (selector) ; 3. vehicle-model : Vehicle -> String (selector) ; 4. vehicle-year : Vehicle -> Number (selector) ; 5. vehicle? : Anything -> Boolean (predicate)
The constructor make-vehicle makes a new Vehicle, as we just saw.
> (vehicle-company rachaels-vehicle) "Chrysler" > (vehicle-model rachaels-vehicle) "PT Cruiser" > (vehicle-year rachaels-vehicle) 2005
> (vehicle? brians-vehicle) #true > (vehicle? marks-vehicle) #true > (vehicle? "geology") #false > (vehicle? "Camry") #false
(string-length (vehicle-model marks-vehicle)) = ??? = ??? = ??? (string-append (vehicle-company brians-vehicle) "thon") = ??? = ??? = ??? (+ (vehicle-year rachaels-vehicle) 3) = ??? = ??? = ???
Exercise 10. List the signatures for each of the courtesy functions for the book structure.
Exercise 11. List the signatures for each of the courtesy functions for the instructor structure.
4 Templates from structures
; process-vehicle : Vehicle -> ... ; ... (define (process-vehicle v) (... (vehicle-company v) ... (vehicle-model v) ... (vehicle-year v) ...))
The template for processing a structure reminds us to use each field.
Exercise 12. Does the template for processing Departments have a conditional? Does the template for processing Vehicles have a conditional? Why the difference?
Exercise 13. Write a template for processing a book.
Exercise 14. Write a template for processing an instructor.
Exercise 15. Point out where the data definition for an instructor refers to the data definition for a department. Does your template for processing an instructor refer to the template for processing a department in the corresponding place? It should.
5 Functions on structures
; praise-vehicle : Vehicle -> String ; Returns a sentence that praises the given vehicle. (check-expect (praise-vehicle brians-vehicle) "Toyota Camry, a car you can trust.") (define (praise-vehicle v) (string-append (vehicle-company v) " " (vehicle-model v) ", a car you can trust."))
We can fill in the same template differently to design another function upgrade-vehicle which upgrades the vehicle by three years:
; upgrade-vehicle : Vehicle -> Vehicle ; Upgrades the given vehicle by three years. (check-expect (upgrade-vehicle brians-vehicle) (make-vehicle "Toyota" "Camry" 2008)) (define (upgrade-vehicle v) (make-vehicle (vehicle-company v) (vehicle-model v) (+ (vehicle-year v) 3)))
Exercise 16. Design a function tome? which takes a book and returns whether it has more than 300 pages.
Exercise 17. Design a function equalize-salary which takes an instructor and returns a new instructor like the given one, but with their salary changed to the salary for their department. Use the salary function provided above.
6 Challenge: a chase
Let’s design an interactive animation where a pet chases a goal. As time passes, the pet gets closer and closer to the goal. But using the mouse, you’ll be able to move the goal to a new location.
Exercise 18. To use big-bang, always ask first: what is a World? Develop a data and structure definition for a World that stores the pet’s location and the goal location as two Posns.
Exercise 19. Define two examples of Worlds.
Exercise 20. Write a template for processing a World.
Exercise 21. Point out where the data definition for a World refers to the data definition for a Posn. Does your template for processing a World refer to the template for processing a Posn in the corresponding places? It should.
Exercise 22. Feel free to skip this exercise and do the next one, or do this exercise and skip the next one. Take someone else’s code for the exercise you skipped, but you must first agree with them on exactly what a World is.
Design a function that takes a World and draws it as an image. Put one sprite at the pet’s location, and put another sprite at the goal location. The template for processing a World should guide you to design a helper function that places a given sprite at a given Posn on a given background image.
Exercise 23. Design a function that takes a World and returns a new World in which the goal stays put but the pet moves towards the goal. The pet’s new location should be the midpoint between the pet’s old location and the goal location. The template for processing a World should guide you to design a helper function that computes the midpoint of two given Posns.
Exercise 24. Design a function that takes a World, two numbers, and a MouseEvent as inputs, and returns a new World in which the pet stays put but the goal moves to the two given numbers. You might prefer to move the goal only for certain MouseEvent values.
The first input to big-bang must be a World.
The function you give to to-draw must have the exact signature World -> Image
The function you give to on-tick must have the exact signature World -> World
The function you give to on-mouse must have the exact signature World Number Number MouseEvent -> World