On this page:
Welcome to Lab
How Pairs Work
Launching Dr  Racket
Interacting with Dr  Racket
You’ve Got Errors
Conditions
Programming with Images
Working on Problem Sets

Lab 1 BSL in DrRacket

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.

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.

DrRacket comes with three important tools:
  1. the Definitions area, where you define functions, state tests, and write down expressions your program must evaluate;

  2. The Interactions area, where you ask DrRacket to evaluate expressions and to experiment with ideas;

  3. The Stepper, which helps you step through the evaluation of function definitions and expressions from the Definitions area.

When you walk out of the lab, you must feel comfortable using all three tools. Computer scientists tend to speak of the tool chain or occasionally the tool box, and proficient programmers must know their tool chain well.

image

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

3 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

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.

Define the following function in the definitions area:

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.

Run. Interact. Add an application of how-far to the definitions area. Use the stepper.

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

You’ve Got Errors

When you program, you encounter three kinds of 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.

Enter a solution to the students’ problem and demonstrate all three of these errors. Explain that testing—to be explained in the future—is a mechanism to defend yourself against logical errors.

Switch partners

Conditions

Define a conditional function.

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.

Explain cond carefully. Explain brackets as an alternative for parentheses that visually set apart cond clauses. Interact. Use the stepper.

15 min

It is your turn.

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. Interactively determine its width, height, area. Compose car image with squares, circles, triangles. Draw red frame around it.

Program a function that processes images.

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

Interact. Use the stepper.

Switch partners; 15 min

Draw some pictures yourself

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.

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.

Time to solve one last problem Switch partners

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".