Lab 1 The Basics
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 Monday!).
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.
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 4 Define a function multiple-of-5? that accepts a number and determines if it is a multiple 5.
Exercise 5 Ralph wrecks the windows on his building at a rate of 4 windows per minute. After 5 minutes he has 20 windows left to break. Define a function wreck-it which, given the of minutes since he started wrecking, produces the number of windows in the building that are left for him to break.
Exercise 6 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. This can be any greeting you choose.
Exercise 7 Call these functions in the interactions window. What happens if you call (multiple-of-5? "cat") or (greet #t)?
Exercise 8 Run the stepper on your program. Follow it through to see how it evaluates the tests.
Exercise 9 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 10 After determining this is indeed incorrect, what could the author of this code have done to catch their mistake?
Conditions
Goals: Get practice writing conditional functions.
Exercise 11 Define the function absolute which consumes a number and produces the absolute value of that number (do not use abs).
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 A flatscreen TV costs $140. However, if you buy them in bulk they cost only $65 each. Define a function num-tvs which, given an amount of money, produces the number of TVs you can buy. In order to buy TVs in bulk you must buy at least 300 of them. For example, given $10,000 you could buy 71 flatscreens, but given 50 million dollars you could buy 769,230 flatscreens.
Exercise 14 When using a laptop to take notes students remember 80 percent of the material for the first 24 hours and only 40 percent after that. When using hand-written notes students remember 70 percent of the material regardless of how long it has been since they wrote them. Write a function percent-remembered that, given the number of hours since lecture and a boolean indicating whether the person took hand-written notes, will produce the percentage of the material that person will remember.
Note: These numbers are totally made up but there are several articles here and here about the long-term benefits of hand-written notes.
Programming with Images
Goals: Get practice programming with images and writing a simple animation.
(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.
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?.