Lab 1 - BSL in DrRacket
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. You will also learn how to submit assignments starting with Problem Set 2.
BSL (Beginning Student Language) is a programming language that you'll start working with (we'll use more advanced languages as the course progresses). DrRacket is an Interactive Development Environment (IDE) where you will develop your programs.
the Definitions area: where you define functions, tests, and expressions that need to be evaluated. When you click Save, everything you've written in your definitions area is saved in a .rkt file.
The Interactions area: where you experiment with ideas by writing expressions that DrRacket can evaluate. This area is like scrap paper, nothing your write here is saved when you click save.
The Stepper: a tool that helps you step through the evaluation of function definitions and expressions from the Definitions area.
Welcome to Lab
Collect contracts from all students who enter the room. Hand contracts to co-pilot TA who matches up pairs on a random basis.
How Pairs Work
You will be working in pairs during labs. Pairs consist of a pilot and a co-pilot. The pilot types at the keyboard while the co-pilot looks over the pilot’s shoulder. Like in real airplanes, pilots and co-pilots switch back and forth during the lab.
Launching DrRacket
Launch DrRacket. It is an interactive development environment aka an IDE. You can use it for many different programming language: Algol 60, Beginning Student Language aka BSL, Datalog, Racket, R6RS Scheme, and a few more. Choose the Beginning Student Language from the {Language | Teaching Languages} drop-down menu.
Interacting with DrRacket
(45 min) Use DrRacket’s interactions area as a calculator. Show a range of operations on numbers, booleans, strings. Use big numbers, use complex numbers. In the future, you don’t need a calculator; use the interactions area. It is your time to play. Re-arrange room so partners get to work with each other. Co-pilot TA must have finished pair assignment now.
Use DrRacket’s definitions area to write down expressions. Nothing happens. Show run. Show the stepper. Time to play again.
Sample Problem The function how-far consumes the number of minutes you drive at 55mph and produces how far you get in the given time.
Define a function that accepts a number of minutes and computes how many whole hours these minutes represent.
You’ve Got Errors
syntax errors: these are errors that are the programming equivalent of "bad grammar", meaning that what you wrote is not a valid BSL “sentence” DrRacket will show you these errors in red text.
run-time errors: these are errors that occur when you write code that is grammatically correct but stops your program from finishing its execution. These errors include things like trying to divide by zero or if you give a function too many arguments or too few arguments.
logical errors: these are errors that occur when you write code that is grammatically correct and will execute, but doesn't perform the task that you intended it to perform. These errors do not trigger any error messages and thus can be more difficult to catch.
Switch partners
Conditions
Sample Problem The function how-hot consumes a temperature (number) 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.
Sample Problem Define the function letter-grade. It consumes a score (number) between 0 and 100 and produces a letter grade ("A", "B", "C", "D", or "F"). You may choose your own scale.
Programming with Images
(30 min) Use (require 2htdp/image). Show circles, squares, text. Show “addition” of images.
Find an image of a car on the web. Copy and paste it into DrRacket’s definitions area and give it a name. Determine its width, height, area. Compose a car image with squares, circles, triangles. Draw a red frame around it.
Sample Problem Define the function frame, which consumes an image and draws a red frame around it.
Switch partners; 15 min
Sample Problem 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 text.
Sample Problem Define a program 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 a bit longer, say up to 10, it’s dubbed 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".