On this page:
Stepping, Debugging Tip
From Structure Type Definitions to Data Definitions
Templates
From Templates to Functions
Structuring the World
Before you go..

Lab 2h Structures

home work!

Purpose The purpose of this lab is to practice the use and definition of structure types. Also, we will begin to act like program designers and software engineers instead of plain programmers.

Hands-on practice is the only way to drill mechanical programming skills, which are important to your success in understanding design skills in this class. Lab is one place to practice; the finger exercises are another. If you are ever stuck with finger exercises, see your tutors, TAs, and instructors.

Stepping, Debugging Tip

If you’re stuck on a difficult error, copy just enough of your program into a new tab such that you can reproduce the error and use the Stepper to see where your program goes off track.

Stepping shows how structures are taken apart and how images are composed. The following program does not pass its test:
(define-struct student (name hobby))
; A Student is a structure:
;  (make-shape String Image)
; where name is the name of the student
; and hobby is an image conveying their favorite hobby
 
; data examples:
(define s1 (make-student "Matt Singer" (text "playing hide and seek" 20 'red)))
 
(define BACKGROUND (rectangle 250 50 "solid" "gold"))
 
; render : Shape -> Image
; Place shape on a 250 x 50 gold rectangle
(define (render s)
  (overlay BACKGROUND (student-hobby s)))
(check-expect (render s1) (overlay (student-hobby s1) BACKGROUND))
Step through with check-expect. Step without check-expect.

From Structure Type Definitions to Data Definitions

Remember structure type definitions from class:

(define-struct name (field ...))

What do structure type definitions do?

Exercise 1 List the functions that the following sample definitions define:
(define-struct photo (image tag))
(define-struct 3d (x y z))
(define-struct ta (last given lab))

Recall that a data definition for structured data states explains in a mixture of English and BSL how to construct elements of this class of data. In particular, it specifies what kind of data each field contains. Refer to Section 5.4 of HtDP2e if you need to review.

Exercise 2 Here are some data definitions:
(define-struct item (tag price))
; An Item is a structure:
;   (make-item String PositiveNumber)
 
(define-struct phd (name grant pay-rate))
; A PhD student is (represented by) a structure:
;   (make-phd String GrantId PositiveNumber)
; A GrantId is one of:
;  "1-123"
;  "3-AB4"
;  "9-999"
Create at least two data examples per data definition, and for each structure, list the functions they add to the environment as well as their signatures.

Exercise 3 The Boston Zoo keeps track of information for every animal that is kept there. For each animal, they store its name, species, age, breakfast hour, and dinner hour (if they don’t get fed twice a day, they try to eat the visitors...).

Define a structure type Animal for representing information about a zoo animal.

Now formulate a data definition for your structure type definition. Pay careful attention to the times at which the animal is fed. Might this warrant a new data definition?

Exercise 4 The Zoo keeps track of each animal’s food in a structure that contains the name of the food and its experation date as strings. Create a structure type Food, and extend your definition for Animal to include the kind of food it eats.

Templates

Switch roles

What are templates? Again, see Section 5.4 of HtDP2e if you forgot.

Exercise 5 Construct a template for functions that process Items.

Exercise 6 Construct a template for functions that process PhDs.

Exercise 7 Construct a template for functions that process Animals.

From Templates to Functions

Let’s start with a couple of exercises on designing functions that work on the above structure types.

Exercise 8 Design the function re-assign. It consumes two pieces of data: a PhD student and a GrantId. The result is a new PhD whose grant field contains the new GrantId regardless of what was in there before.

Exercise 9 Design distance0. The function consumes an instance of 3D:
; A 3D is a structure:
;   (make-3d Number Number Number)
; interpretation: (make-3d a b c) represents a point in
; 3-dimensional space using Cartesian coordinates
It computes the distance of the given point to the origin of the space.

Hint Your math friend reminds you that the distance is computed as the square root of the sum of the squares of the coordinates.

Switch roles Templates are outlines for functions, and as the above exercises emphasize, they can be developed without knowing the purpose of the function. Let’s use the templates for Animals to code.

Exercise 10 When an Animal has a birthday, the zoo needs a function that takes an animal and returns a new animal with the same contents except with 1 added to its age.

Exercise 11 To ensure the safety of zoo visitors, design a function that consumes an Animal and the current hour and returns whether it’s mealtime.

Exercise 12 In preparation for next April Fool’s Day, the system manager of the zoo wants you to design a function that takes an Animal and returns a new Animal with the same data contents except with its age converted to dog years. Note: there are 7 dog years in 1 human year.

Okay, no one thinks the system manager is a good prankster but you are working for him, and you have no choice but to follow his orders.

Structuring the World

Let’s design a simple “game”, where a 10-pixel red circle chases the mouse pointer around the screen, with a 5-pixel blue square right under the mouse. What information should we store in the world state?

Switch roles

Exercise 13 Complete the definition and template of a World. Any new structures defined for the data definition of a World should have their own complete definition and templates.

Exercise 14 Design a function draw that consumes a World and returns a drawing of the current world state.

Have you been writing tests?

Exercise 15 Design a function keepaway that will be given to the on-mouse handler in big-bang. Every time the mouse moves, what should change in the world state? Read the docs for on-mouse to see what signature keepaway will need.

Exercise 16 Design a function chase to move the circle along as time passes. For now, let the circle only ever move either horizontally or vertically: it should move in whichever direction brings it closest to the mouse position?

Really, now, have you been writing tests?

Exercise 17 Design a function stop that will return true when the circle has reached the mouse position..

Exercise 18 Kick off your game with an appropriate call to big-bang. What should the initial world be? Think carefully – you might not have enough information!

Exercise 19 Challenge: Move the square to the circle along a straight line at a fixed speed.

Before you go..

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We aren’t hungry zoo animals!