On this page:
Introduction
Structure Definitions
Templates
The Stepper
From Templates to Functions
Before You Go...
6.6

Lab 2 Structural Integrity

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 just plain programmers.

Textbook references: Chapter 5: Adding Structure

Introduction

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 one. If you are ever stuck with finger exercises, see your tutors, TAs, and instructors.

Structure Definitions

Goals: Practice understanding structure definitions and the functions they define. Practice creating structures from a definition.

Sample Problem Define a structure for an employee. An employee has a first name, a last name, an hourly wage, and a social security number.

; An Employee is a (make-employee String String Number Number)
(define-struct employee (first last wage ssn))
; - where first is the employee's first name
; - last is the employee's last name
; - wage is the employee's hourly wage
; - and ssn is the employee's social security number

Sample Problem List out the functions given by the structure definition for an employee.

; Predicate: employee?
; Constructor: make-employee
; Selectors: employee-first, employee-last, employee-wage, employee-ssn

Sample Problem Write the template for an Employee.

; employee-temp : Employee -> ???
(define (employee-temp e)
  (... (employee-first e) ...
       (employee-last e) ...
       (employee-wage e) ...
       (employee-ssn e) ...))

Exercise 1 List the functions that the following sample definitions define:

; A Photo is a (make-photo Image String)
(define-struct photo (img tag))
; - where img is the actual image
; - and tag is an identifying label for the image
 
; A 3D is a (make-3d Number Number Number)
(define-struct 3d (x y z))
; - where x is the x-coordinate of the point
; - y is the y-coordinate of the point
; - and z is the z-coordinate of the point
 
; A TA is a (make-ta String String Number)
(define-struct ta (last given lab))
; - where last is the TA's last name
; - given is the TA's first name
; - and lab is the lab number this TA leads

Exercise 2 Create at least two data examples for each of the data definitions below:

; An Item is a (make-item String PositiveNumber)
(define-struct item (tag price))
; - where tag is the name of the item
; - and price is the price of an item
 
; A PHDStudent is a (make-phd String GrantId PositiveNumber)
(define-struct phd (name grant pay-rate))
; - where name is the full name of the student
; - grant is the grant ID number of their current grant
; - and pay-rate is their hourly wage
 
; A GrantId is one of:
; - "1-123"
; - "3-AB4"
; - "9-999"

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 and formulate a data definition for your structure type definition.

Templates

Goals: Practice creating templates from a structure definition.

Exercise 4 Construct a template for functions that process Photos.

Exercise 5 Construct a template for functions that process 3Ds.

Exercise 6 Construct a template for functions that process TAs.

Exercise 7 Construct a template for functions that process Items.

Exercise 8 Construct a template for functions that process PHDStudents.

Exercise 9 Construct a template for functions that process Animals.

The Stepper

Sample Problem Design the function paid-enough? which takes an Employee and determines whether they are making more than minimum wage (in Massachusetts the minimum wage is $10/hr, or at least it was when this lab was written).

; paid-enough? : Employee -> Boolean
; Is this employee making more than minimum wage?
(define (paid-enough? e)
  (> (employee-wage e) 10))
(check-expect (paid-enough? (make-employee "Al" "Bennet" 15 123456789)) #true)
(check-expect (paid-enough? (make-employee "Charles" "Damore" 5 102030405)) #false)

Sample Problem Design the function related? which takes an Employee and someone’s last name and determines if the employee has the same last name.

; related? : Employee String -> Boolean
; Does this employee have this last name?
(define (related? e last-name)
  (string=? (employee-last e) last-name))
(check-expect (related? (make-employee "Al" "Bennet" 15 123456789) "Al") #false)
(check-expect (related? (make-employee "Charles" "Damore" 5 102030405) "Damore") #true)

From Templates to Functions

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

Exercise 11 Design distance0. The function consumes an instance of 3D and computes the distance from 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.

Exercise 12 Design the function birthday which takes an Animal and returns a new Animal with the same contents except with 1 added to its age.

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

Exercise 15 Sometimes the animals in the zoo have babies! Design a function which consumes an Animal and produces a new animal representing the new baby. The baby should have the same species and feeding schedule as the parent but a new name. The zoo staff are not very creative so the new name should be the parent’s name with the suffix “Jr.” at the end. For example, an animal named Bob would have a child named Bob Jr.

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!