Lab 1h 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.
BSL programming is basically prefix syntax for pre-algebra plus a range of pre-defined functions. In contrast to your pre-algebra courses, your BSL expressions and functions definitions are mechanically checked and, if they are found to be ill-formed, you get an error message. Getting an error message is good. All novice programmers make mistakes. The key is that by the time you walk out of this lab, you must have seen error messages, you must know how to read them and how to react to them.
the Definitions area, where you define functions, state tests, and write down expressions your program must evaluate;
The Interactions area, where you ask DrRacket to evaluate expressions and to experiment with ideas;
The Stepper, which 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. Co-pilot: this is your job.
How Pairs Work
2 min
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
1 min
Launch DrRacket. It is a 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.
Your turn to do an exercise. 5 min
Exercise 1 Define a function that accepts a number of minutes and computes how many whole hours these minutes represent.
You’ve Got Errors
syntax errors, meaning what you wrote is not a BSL “sentence;”
run-time errors, that is, you wrote a BSL sentence but when you interact with it, it signals an error because a function is applied to too many or too few arguments or the wrong kinds of arguments and so on;
logical errors, which does not manifest itself as some red text in the interactions area. Instead, you apply a function and it gives you a response that is wrong.
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.
8 min
Exercise 2 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. Interact. Use the stepper.
Exercise 3 Change your function letter-grade to produce examples of syntax errors, run-time errors, and logical errors. Examine any error messages you get back. Discuss whether you tested your original letter-grade function enough to protect against all logical errors.
Switch partners; 8 min
Exercise 4 Define a function that calculates sales tax. The function consumes the sale price and the tax percentage (as a decimal or fraction) and produces the final price. For instance, if given 20 and 1/20 it should compute 105% of $20, or 21.
Exercise 5 Define a function that calculates conditional sales tax: only prices $100 or more are taxed. The function consumes a sale price and a tax percentage and produces the final price. For instance, if given 20 and 1/20, it computes $20 (no tax). But if given 200 and 1/20, it computes $210 (5% tax). Hint: Use your program from the previous exercise.
Did you write tests for all the branches of your code from the last two exercises?
Working on Problem Sets
10 min
Work out the solution to an individual problem in a tab by itself, then copy and paste the solution into a solution file for the problem set. Make sure the latter still runs.
Exercise 6 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".
Programming with Images
20 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. Interactively determine its width, height, area. Compose car imagine with squares, circles, triangles. Draw red frame around it.
Sample Problem Define the function red-frame, which consumes an image and draws a red frame around it.
Switch partners; 12 min
Exercise 7 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.
Animation
20 min
Use (require 2htdp/universe). We can create animations using the "universe" module using either animate or big-bang. Here we focus on big-bang which allows us to do more than animate.
The universe module allows you to model a world. Every time the clock ticks, big-bang uses one of your functions to update or create a new world, which becomes the current world. It can use another one of your functions to create an image of your world.
; World is a positive number (current time)
; World -> Image ; draw the image for snapshot time w in the middle ; of a 200x200 scene (define (world-draw w) (place-image (circle w "solid" "black") 100 100 (empty-scene 200 200)))
Your turn again Switch partners
Look up big-bang in Help Desk. Run the following code:
; We start at time 1, use next-world to update the world at each ; tick, and use world-draw to render the world as a scene: (big-bang 1 (on-tick next-world) (to-draw world-draw))
Exercise 8 Right now the circle grows forever. Your task is to stop it from growing when it gets too big for the canvas. Hint: Look up stop-when in Help Desk.
Exercise 9 Change the animation above so that instead of growing larger, the disk moves across the canvas. Try left to right (x), top to bottom (y), and diagonal (both x and y).