On this page:
Introduction
Structure Definitions
Templates
The Stepper
From Templates to Functions
A Little Game
An Introduction To Design
Before You Go...
6.10

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 Define an example of an Employee.

(define EMP-MATT (make-employee "Matt" "Singer" 666 42))

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 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 Define at least two data examples for each of the data definitions below. Note that examples of items will help you make examples of sales!

; 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 Sale is a (make-sale Item [0, 1])
(define-struct sale [item percent-off])
; - where item is the item that is on sale
; - and percent-off is the percentage off of the item in decimal form     

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 Define a template for functions that process 3Ds.

Exercise 5 Define a template for functions that process TAs.

Exercise 6 Define a template for functions that process Animals.

Exercise 7 Define a template for functions that process Items.

Exercise 8 Define a template for functions that process Sales. Note: this template is slightly more special than the others you have defined. Why is that? Ask a staff member if you’re stumped. Hint: it has to do with how examples of items helped make examples of sales.

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)

Exercise 9 Use the stepper to see how these functions are evaluated. It may be helpful to move only the necessary code to a new tab so there aren’t too many steps to look through.

From Templates to Functions

Exercise 10 Define 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 11 Define the function birthday which takes an Animal and returns a new Animal with the same contents except with 1 added to its age.

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

Exercise 13 In preparation for next April Fool’s Day, the system manager of the zoo wants you to define 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 14 Sometimes the animals in the zoo have babies! Define 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 define, an animal named Bob would have a child named Bob Jr.

Exercise 15 Define a function that given a Sale outputs the cost of the item after the sale is applied.

A Little Game

We are going to make a little game that lets someone use the arrow keys to move a dot on a screen.

(require 2htdp/image)
(require 2htdp/universe)
; A LocationWorld (LW) is a (make-posn Number Number)
; and represents where our dot is on the screen in pixel coordinates
; (define-struct posn [x y]) <- note, we don't need this! posns come with BSL
(define LW-1 (make-posn 20 20))
 
; lw-temp : LW -> ?
(define (lw-temp lw)
  (... (posn-x lw) ... (posn-y lw) ...))

Exercise 16 Define a graphical constant DOT that will be useful for our game.

Exercise 17 Define a function draw-location-world that given an LW will draw it on an empty-scene. Use the handy constant you just defined.

Exercise 18 Define a function move-up that takes an LW and a Number and moves the posn up by that amount.

Exercise 19 Define a function move that takes an LW and a String and moves the LW up by some amount if the string is equal to "up". Otherwise, it remains the same. Hint: you can use string=? to test if the given string is equal to "up". Remember, since this function behaves differently based on what the input is, it will need to use cond.

(big-bang LW-1
          [to-draw draw-location-world]
          [on-key move])

Exercise 20 Copy the above code into your program and click Run. Does it work as expected? Change what needs to be fixed (if anything) until it does.

Exercise 21 Extend the game to use all the arrow keys. Then show it off to your friends and family because you just wrote a fun game!

Exercise 22 We’re feeling frisky. Let the player also use the WASD keys!

An Introduction To Design

Exercise 23 If you have any time left over, or for good practice after lab, go back and give signatures and purpose statements to all of the functions you’ve defined above. For examples of signatures and purpose statements, look at the sample problems in "The Stepper" section. You’ll be seeing and writing a lot more of these very soon!

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!