On this page:
Introduction (6:  00 – 9 minutes)
Using Dr  Racket (6:  09 – 9 minutes)
Conditions (6:  18 – 18 minutes)
Homework server (6:  36 – 9 minutes)
Programming with Images (6:  45 – 20 minutes)
Structure Definitions (7:  05 – 15 minutes)
Programming with structures (7:  20 – 20 minutes)
Before You Go...
6.7

Lab 1 The Basics

home work!

Purpose: The purpose of this lab is to give you some hands-on experience with the BSL programming language and with the DrRacket programming environment for BSL, up to programming with conditionals and structures.

Textbook references: Preface (DrRacket and the Teaching Languages); Prologue: How To Program; Chapter 1: Arithmetic; Chapter 2: Functions and Programs (up to but not including 2.5); Chapter 5: Adding Structure.

Introduction (6:00 – 9 minutes)

Goals: Create a CCIS account. Find a partner and exchange contact information.

Exercise 1 Create a CCIS account by following these instructions. If you run into any problems raise your hand and a tutor will come by to help you.

Exercise 2 Find a partner to work with in the lab and on homeworks (the person next to you for instance). Exchange contact information with your partner so that you can meet up outside of the lab. Register your partner with the lab staff.

Using DrRacket (6:09 – 9 minutes)

Goals: Interact with Dr. Racket’s definitions window, interactions window, and stepper. Create basic expressions and function definitions. Introduce different types of errors. Use F1 to find documentation of functions and keywords.

Sample Problem Define a function that accepts a number of minutes and computes how many whole hours these minutes represent.

(define (minutes->hours num-minutes)
  (/ num-minutes 60))

Sample Problem Define the function how-far which consumes the number of minutes you drive at 55mph and produces how far you get in the given time.

(define SPEED 55)
(define (how-far num-minutes)
  (* (minutes->hours num-minutes) SPEED))

Conditions (6:18 – 18 minutes)

Sample Problem Define the function how-hot which consumes a temperature and produces one of three strings: "cold" for temperatures below 45 (inclusive), "comfortable" for temperatures between 45 (exclusive) and 75 (inclusive), and "hot" for temperatures above 75.

(define (how-hot temp)
  (cond [(<= temp 45) "cold"]
        [(<= temp 75) "comfortable"]
        [else "hot"]))

Sample Problem Define the function absolute which consumes a number and produces the absolute value of that number.

(define (absolute n)
  (cond [(< n 0) (- n)]
        [else n]))

Exercise 3 Define the function letter-grade. It consumes a score (number) between 0 and 100 and produces the corresponding letter grade ("A", "B", "C", "D", or "F"). You may choose your own grading scale.

Exercise 4 Define the function good-weather? which consumes a String representing the current weather and produces a Boolean which is true if the weather is good and false otherwise. You may determine what good weather means to you. (For example, course TA Justin Slepak thinks that "sunny" and "blizzard" both describe good weather; but TA Jonathan Schuster is less positive on the subject of snow storms.)

Exercise 5 Define a function that consumes a String and judges how long it is. If the string is shorter than 3 characters, we call it stubby. If it is up to 10 characters, it’s a shorty. For strings, up to 25 characters, we go with middling. And for a string that’s even longer, the function says it is "too wordy".

Homework server (6:36 – 9 minutes)

Goal: Get set up so you can submit homework assignments using Dr. Racket.

Exercise 6 Go to the "Problem Sets" page on the course web site. Read the sub-page about "Online Management," and find the handin-server plugin. Install it in your Dr. Racket.

"Lab-test A" is a fake homework assignment, specifically set up to permit you to test out the homework server—now, before you are trying to meet the deadline for an actual homework assignment.

Submit your solutions to the previous exercises as your solution to "lab-testa" to test out your setup. (Don’t worry: we won’t grade your submission. We won’t even look at it, actually.)

Programming with Images (6:45 – 20 minutes)

Note: You will need to require the 2htdp/image package at the beginning of your program.

Sample Problem Define the function red-frame, which consumes an image and draws a red frame around it.

(define (red-frame img)
  (overlay img (rectangle (+ 1 (image-width img))
                          (+ 1 (image-height img))
                          "solid" "white")
           (rectangle (+ 4 (image-width img))
                      (+ 4 (image-height img))
                      "solid" "red")))

Exercise 7 Define the function big-blue-text. It accepts a string of text, and produces an image of that string, in a large (36 pixels high), blue font. (You may want to read up on the text function in Dr. Racket’s help desk.)

Exercise 8 Define the function sale. It accepts a wish in the form of a String and, if it recognizes the string, it returns an image of this object; otherwise it produces "sorry" as a large image of the text (...in blue, if you like). You may decide which wishes to accept. You can use images from the web or build the images yourself using circles, squares, and other shapes that you can draw in Dr. Racket.

Exercise 9 Define the function on-top which takes two Images and overlays one on the other such that the tops of the images line up.

Structure Definitions (7:05 – 15 minutes)

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

As they may recall (or not) from class, a structure can be defined using this syntax: (define-struct name (field ...))

Reiterate that a data definition for structured data 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

List out the functions that this structure gives you. Make sure the students know that each structure definition gives them a predicate (e.g. posn?), some selectors (e.g. posn-x and posn-y), and a constructor (e.g. make-posn).

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))
; - first is the employee's first name
; - last is the employee's last name
; - wage is the employee's hourly wage
; - 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

Exercise 10 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 11 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 12 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.

Programming with structures (7:20 – 20 minutes)

Exercise 13 Write 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 the given id regardless of what was in there before.

Exercise 14 Write distance0. The function consumes an instance of 3D and computes the distance from the given point to the origin of the space. Reminder: the distance is computed as the square root of the sum of the squares of the coordinates.

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

Exercise 16 To ensure the safety of zoo visitors, write a function that consumes an Animal and the current hour and returns whether it’s meal time.

Exercise 17 In preparation for next April Fool’s Day, the system manager of the zoo wants you to write 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 18 Sometimes the animals in the zoo have babies! Write 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!

Make sure you exchange contact information with your lab partner, as you will be working together on problem sets and in labs in the future. If you have any questions regarding the lab or the course please feel free to ask a TA or tutor.