On this page:
1 Data definitions of structures
2 Examples of structures
3 Courtesy functions for structures
4 Templates from structures
5 Functions on structures
6 Challenge problem
8.6.0.14

Lab 4: Structures

Lectures are very helpful. Labs are good to go to, but MAKE SURE YOU DO THE PROBLEM SETS THEY ARE VERY IMPORTANT!

Note: Whenever you design or write a function, you need to follow the design recipe.

In Lab 2, we started practicing the design recipe. In Lab 3, we applied the design recipe to data defined by enumeration:
;;; 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. 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. (Come back to this exercise later if your food-truck colleagues are waiting.) 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.

Exercise 5. (Come back to this exercise later if your food-truck colleagues are waiting.) Develop a data and structure definition for a School, which holds three departments. Does your data definition for a School 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 6. Is the following an example of a Vehicle? What happens when DrRacket runs this definition? Why?

(define kens-vehicle (make-vehicle "Bicycle" 1982))

Exercise 7. 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 8. Define two examples of books.

Exercise 9. Do this exercise in groups of at least 2 people.

Remember your food truck in Lab 2: The design recipe and Lab 3: Multiple cases? To offer more customization, you decided to change the menu again. Write a new data and structure definition for an Order. It should start like this:

; An Order is (make-

(define-struct

To give an Order, a customer must assign each field some value. For instance, if your fields are the radius of a pizza and the flavor of a drink, then everyone must order both a pizza and a drink—not a pizza by itself, and not a drink by itself.

Define two examples of Orders.

Exercise 10. Define two examples of instructors.

Exercise 11. Define two examples of Schools.

3 Courtesy functions for structures

When DrRacket runs the structure definition (define-struct vehicle ...), it defines the following five courtesy functions.
; 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.

Each selector retrieves the data stored in a given field. There are as many selectors as there are fields.
> (vehicle-company rachaels-vehicle)
"Chrysler"
> (vehicle-model rachaels-vehicle)
"PT Cruiser"
> (vehicle-year rachaels-vehicle)
2005

The predicate vehicle? determines whether a value is in fact a Vehicle:
> (vehicle? brians-vehicle)
#true
> (vehicle? marks-vehicle)
#true
> (vehicle? "geology")
#false

Exercise 12. List the signatures for each of the courtesy functions for the book structure.

Exercise 13. List the signatures for each of the courtesy functions for Order.

Exercise 14. List the signatures for each of the courtesy functions for the instructor structure.

Exercise 15. List the signatures for each of the courtesy functions for School.

4 Templates from structures

From the Vehicle data definition, we can design the following template to process Vehicles.
; 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 16. Does the template for processing Departments have a conditional? Does the template for processing Vehicles have a conditional? Why the difference?

Exercise 17. Write a template for processing a book.

Exercise 18. Write a template for processing an order.

Exercise 19. Write a template for processing an instructor.

Exercise 20. 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.

Exercise 21. Write a template for processing a School.

Exercise 22. Point out where the data definition for a School refers to the data definition for a department. Does your template for processing a School refer to the template for processing a department in the corresponding places? It should.

5 Functions on structures

And finally, from the template for a Vehicle structure, we can fill in the ellipses to design a function praise-vehicle which generates a string to praise a vehicle:
; praise-vehicle : Vehicle -> String
; Returns a sentence that praises the given vehicle.
(define (praise-vehicle v)
  (string-append (vehicle-company v)
                 " "
                 (vehicle-model v)
                 ", a car you can trust."))
(check-expect (praise-vehicle brians-vehicle) "Toyota Camry, 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.
(define (upgrade-vehicle v)
  (make-vehicle (vehicle-company v)
                (vehicle-model v)
                (+ (vehicle-year v) 3)))
(check-expect (upgrade-vehicle brians-vehicle) (make-vehicle "Toyota" "Camry" 2008))

Exercise 23. Design a function tome? which takes a book and returns whether it has more than 300 pages.

Exercise 24. Do the rest of this lab in groups of at least 2 people. Also, feel free to do this exercise and the next exercise in parallel. However, it is very important that you use the same data definition for an Order that you wrote in Exercise 9.

Design a function price-order that takes an Order and returns its price (a number).

Exercise 25. Design a function draw-order that takes an Order and returns a crude image of the food.

Exercise 26. Go visit another group and try out their menu:
  1. Read their data definition carefully, but don’t bother reading the rest of their code.

  2. Make an Order. Make sure to obey their data definition for what an Order is.

  3. Give your order to the draw-order function in their Interactions Window. Is it appetizing?

  4. Give the same order to the price-order function in their Interactions Window. Is it worth it?

Exercise 27. Design a function equalize-salary which takes an instructor and returns another instructor with their salary changed to the salary for their department. Use the salary function provided above.

Exercise 28. Design a function average-salary which takes a School and produces the average salary of its three departments. Use the salary function provided above.

6 Challenge problem

Design an interactive animation of a rocket launch as follows. When the animation starts, the rocket rests motionless, centered at the bottom of the scene, ready to launch. When the user/player hits any key, the rocket launches and flies straight upwards. Once the rocket is flying, it should ignore any further launch requests (keypresses); but it doesn’t. Unfortunately, a keypress while the rocket is in-flight in fact causes it to malfunction and explode. The user should then see an explosion of some sort (say, a brightly colored circle) at the last position of the rocket.

The key challenge in this problem is to come up with a good data definition for the state of the rocket, which can be used by big-bang as a World. If you’re having trouble getting started, feel free to consult section 4.5 of the textbook (which discusses a rocket launch program in detail) and lecture 7 of the course (which introduces big-bang).