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 Putting it together:   a chase
8.12

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. 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🔗

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 9. List the signatures for each of the courtesy functions for the book structure.

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

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 11. Does the template for processing Departments have a conditional? Does the template for processing Vehicles have a conditional? Why the difference?

Exercise 12. Write a template for processing a book.

Exercise 13. Write a template for processing an instructor.

Exercise 14. 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🔗

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.
(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 15. Design a function tome? which takes a book and returns whether it has more than 300 pages.

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

6 Putting it together: 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 17. 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 18. Define two examples of Worlds.

Exercise 19. Write a template for processing a World.

Exercise 20. 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 21. 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 22. 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 23. 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.

Exercise 24. Assemble the three functions you just designed into a big-bang animation. Remember that
  • 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

To slow down the pet so you can see more clearly, you can give on-tick an additional number that says how many seconds per tick.