On this page:
Introduction & Setup
How Labs Operate
Using Dr  Racket
Conditions
Programming with Images
Before You Go...
Pixar Whomst?
6.6

Lab 1 The Basics

lab!

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.

Textbook references: Preface (DrRacket and the Teaching Languages), Prologue: How To Program, Chapter 1: Arithmetic, Chapter 2: Functions and Programs

Introduction & Setup

Goals: Read and sign the course contract.

Exercise 1 Follow the instructions on this page to set up an account on the handin server. You will be using the handin server to submit all your homeworks for the semester (including the homework that’s due Thursday!).

Exercise 2 Read the course contract and sign it on the handin server.

Exercise 3 If you are using your own laptop, download and install DrRacket on your machine. If you are not, count to 10 in your head to keep things fair and balanced.

How Labs Operate

Although your TA went over this in the introduction, you might find it helpful to find this written down. Lab writeups consist of three major parts: starter code, reviewed exercises, and exercises.

Note that for all of these parts, if you are ever stuck or have any questions please raise your hand and a tutor or TA will make their way over to help.

Exercises come with a star rating, from 1-3 stars, indicating the difficulty of the problem.
  • Exercises with * are expected to be straightforward and should only take a minute or two at most.

  • Exercises with ** are problems of average difficulty and cover concepts most recently learned in lecture. The vast majority of exercises in labs will be 2 star problems.

  • Exercises with *** are problems that require creative problem solving, particular insight, difficult syntax, or some combination of the above. Labs will have a few 3 star problems at most, and possibly none.

Note that for all of these ratings, if you are ever stuck or have any questions please raise your hand and a tutor or TA will make their way over to help. If you find yourself repeatedly stuck on ** or especially * problems, it is a sign you should come to office hours for additional practice and help.

Each lab has a Before You Go... section, with a brief note. It is our hope you complete the lab up to this point before the lab time runs out. Most labs continue on past this, though, and the most fun and exciting parts of the lab can always be found at the lab’s completion.

You are free to leave when you have completed the entire lab or the head TA ends lab when time is up.

Using DrRacket

Goals: Interact with Dr. Racket’s definitions window, interactions window, and stepper. Create basic expressions and function definitions. Introduce different types of errors.

Exercise (Reviewed) 4 * Define a function multiple-of-5? that accepts a number and determines if it is a multiple 5.

Exercise (Reviewed) 5 * Define a constant GREETING which contains your greeting. Then, use it in a function called greet that takes a name and outputs a full greeting.

Exercise 6 * Call these functions in the interactions window. What happens if you call (multiple-of-5? "cat") or (greet #t)?

Exercise 7 * Run the stepper on your program. Follow it through to see how it evaluates the tests.

Exercise 8 * Below is a piece of code that will run and all the tests will pass... but is it correct? Hold a brief philosophical discussion about the importance of language and naming in programming with yourself, labmates, and/or course staff to arrive at the answer.

; times-itself : Number -> Number
; Multiply a number by itself
(check-expect (times-itself 0) 0)
(check-expect (times-itself 2) 4)
(define (times-itself n)
  (+ n n))

Exercise 9 * After determining this is indeed incorrect, what could the author of this code have done to catch their mistake?

Exercise 10 * It has been estimated that fixing the water in Flint, Michigan would cost 55 million dollars. Given someone’s net worth, define a function that will determine what percentage of their net worth it would take to fix the water. Elon Musk’s net worth, for example, is 19.7 billion and Jeff Bezos’ is 140.9 billion.

Conditions

Goals: Get practice writing conditional functions.

Exercise (Reviewed) 11 ** Define the function absolute which consumes a number and produces the absolute value of that number (do not use abs).

Starter Code: Below is a data definition that defines what a Score can be.

; A Score is a number in the range [0, 100]                     

Exercise 12 ** Define the function letter-grade. It consumes a Score and produces the corresponding letter grade ("A", "B", "C", "D", or "F"). You may be as wishful as desired in choosing your own grading scale.

Exercise 13 ** Define a function that given a number of hours worked and an hourly wage computes how much money has been earned. Any hours worked over 40 hours must be paid at 1.5 times the given rate.

Exercise 14 ** Define the function runtime that given a movie title outputs its runtime in minutes. Ensure the function works on a handful of inputs of your choosing, and for any other inputs throws an error, which can be tested with check-error.

Programming with Images

Goals: Get practice programming with images and writing a simple animation.

Starter Code: This line of code is required to program with images.

(require 2htdp/image)

Exercise 15 ** Use triangle, square, rectangle, above, and overlay/align to draw yourself a house with a roof and door (and circle if you’re feeling bold enough for a door handle).

Exercise 16 ** Congratulations, you’ve moved up a tax bracket. Define a constant WINDOW and place two of them on your humble home. Note how in using a constant we only have to draw it once and get to use it twice!

Exercise 17 ** Define a function scene that takes in a natural number and places a circle of that radius at the center of a 50x50 empty-scene. Use modulo to ensure the radius always stays below 20.

Starter Code: These lines of code will run your animation.
(require 2htdp/universe)
(animate scene)

Exercise 18 * Launch your animation by pressing run.

Before You Go...

Make sure you can log into the handin server and sign the course contract if you didn’t already do that. If you have any questions regarding the lab or the course please feel free to ask a TA or tutor.

Pixar Whomst?

Goals: Use math (gasp) and conditional programming to make the animation better.

Exercise 19 *** Use cond (or if if you’re feeling cheeky) to make the circle grow and shrink in a loop instead of grow and suddenly dissapear and grow again. Since computing the radius is now a somewhat complex computation, it makes sense to write a seperate helper function which only computes the radius that scene calls. Hint: quotient, even?.