On this page:
Welcome to Lab
Launching Dr  Racket
Interacting with Dr  Racket
You’ve Got Errors
Time For Some Functions!
The Help Desk
Conditions
Working on Problem Sets
Programming with Images
Animation

Lab 1h 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.

Welcome to Lab

Lab is your chance to get some stress-free, hands-on interaction with programming with direct access to your tutors and TAs. Occassionally, TAs will address the entire lab, if they feel the room is having difficulty with a particular problem or if there is something they need to teach for the lab. Tutors will roam the isles, checking in on how you’re doing. Please feel free to ask tutors or TAs any and all questions throughout the lab.

It’s perfectly fine to not finish the lab in the allotted time, and most are written to go past the time. However, it’s a very good idea to at least read the entire lab. Better yet, feel free to finish it at home (or with us in office hours). This way, you’ll know what you are expected of being able to do and get some more practice.

Also, hand in your Course Contract now!

Launching DrRacket

Launch DrRacket. It is a integrated development environment aka an IDE. One can use it for many different programming languages, but we will be using it for Beginning Student Language aka BSL. Choose the Beginning Student Language as your language. This can be done via the lower left corner or by the Language > Choose Language drop down menu.

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 red error message. More on these soon!

In the Edit > Preferences > Colors menu one can customize the colors of DrRacket. There is even a color-blind safe setting built in.

Interacting with DrRacket

DrRacket comes with three important tools, listed in order of their importance:
  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 will feel comfortable using all three tools. Let’s get some practice!

Use DrRacket’s definitions area to write down expressions. Nothing happens... until you click run!

Use DrRacket’s interactions area as a calculator. Show a range of operations on numbers, booleans, and strings. Use small numbers, use big numbers, and especially use your favorite number.

Click on the stepper. What does it do? Find out!

You’ve Got Errors

Now, in your exploration, you may have come across some errors. Getting an error message is good! All programmers make mistakes, and the earlier on you make and detect them, the easier they are to solve! By the time you walk out of this lab, you will have seen error messages, and you will know how to read and how to react to them.

When you program, you encounter three kinds of errors:
  • syntax errors, meaning what you wrote is not a valid BSL expression, similar to how "this sentence a no verb" is not a valid English sentence;

  • run-time errors, that is, you wrote a BSL expression 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 do not manifest themselves as some red text in the interactions area. Instead, you apply a function and it gives you a response that is wrong.

Be sure to purpsefully make and solve all three types of errors. Testing is the safest way to avoid errors!

Time For Some Functions!

Define the following function in the definitions area:

Exercise 1 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.

Exercise 2 People who just drive at 55mph are boring. Write a function how-far-2.0 that consumes the number of minutes you drive and a speed in miles per hour and produces how far you get.

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

The Help Desk

One of your greatest resources in this course, and whenever you are programming, is language documentation. Right click on the name of a pre-defined function (+ for example) and select "Search in the Help Desk" (you can also click on +). The Help Desk contains documentation on every function in BSL and will be a world of help. Ask a tutor or TA if you’re having trouble reading the documentation.

Conditions

Define a conditional function. Make sure to use brackets/parentheses appropriately (if you’re not sure, ask a tutor/TA) as they visually set apart cond clauses!

Exercise 4 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.

Exercise 5 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 6 Change your function letter-grade to produce examples of syntax errors, run-time errors, and logical errors. Examine any error messages you get back. Did you test your original letter-grade function enough to protect against all logical errors?

Exercise 7 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 8 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

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 9 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

Use (require 2htdp/image). Show circles, squares, text. Show “addition” of images.

Find an image of a crying giraffe boat on the web. Copy and paste it into DrRacket’s definitions area and give it a name. Interactively determine its width, height, area. Then, make your own boat with triangles, lines, squares, etc. Give it a flashy name with some text; the most creative name gets a (star 10 'solid 'gold)!

Program a function that processes images.

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

Exercise 11 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

Using (require 2htdp/universe), we can create animations using the "universe" module’s big-bang function.

The universe module allows you to model a world. Based on what you give big-bang, every time the clock ticks, the mouse is used, or a key is pressed, big-bang uses one of your functions to create a new world, which becomes the current world. It uses another one of your functions to produce an image of the world.

Our "world" will represent the number of ticks passed:

; World is a positive number (current time)

Create a function that will calculate the next world:
; World -> World
; calculates the next world (increments the time)
(define (next-world w)
  (add1 w))

Create a function that will create an image snapshot from the world:
; 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)))

; 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 12 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 13 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).

Exercise 14 Do your best to understand big-bang to the point where you can create your own fun animation. Impress your friends, tutors, and TAs with your wonderful creativity. Warning: delightful animations may result in unbridled enthusiasm from your instructors. Feel free to ignore our obnoxious jubilance, but we promise it’s only there because we care!

If you have any questions about the course, computer science, college, or life in general, please come to any of our office hours. We’re here for you! This is the beginning of a brave new world, and we hope you’re as excited as we are.