On this page:
Introduction
Using Dr  Racket
Exercises
Before You Go...
8.5

Lab 0 The Basics

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.

Textbook references: Preface (DrRacket and the Teaching Languages), Prologue: How To Program, Chapter 1: Arithmetic, Chapter 2: Functions and Programs (up to but not including 2.5)

Introduction

Goals: Setup accounts and get started.

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.

Exercise 2 Read and sign the course contract in the Handin Server.

Exercise 3 Download and install DrRacket on your machine. We will be using version 8.12.

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. Use F1 to find documentation of functions and keywords.

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

(define (minutes->hours num-minutes)
  (/ num-minutes 60))

Sample Problem Define the function how-far which consumes the number of minutes you drive at 55mph and produces how far you get in the given time.

(define SPEED 55)
(define (how-far num-minutes)
  (* (minutes->hours num-minutes) SPEED))

Exercises

Exercise 4 For any word of at least one character that starts with a letter, let’s say that its "bingo word" is the uppercase version of the first letter, followed by a space, and then followed by the number of characters in the word. For example, the bingo word of "bingo" is "B 5" and the bingo word of "Win" is "W 3".

Define a function, bingo-word, that takes a string as an argument and returns its bingo word. You may assume that the argument is a valid word as described above.

Don’t forget to include a signature and reasonable purpose statement.

Hint: if you don’t remember ALL the string functions in BSL, that’s ok! Remember that you can right-click on a function and search the Help Desk - as a start, string-append will be quite handy... and now you just need some help with isolating substrings, converting strings to upper case, getting the length of a string, and converting a number to a string.

Exercise 5 Let’s make a pretty animated scene with a house, in parts! Use the triangle, square, rectangle, above, and overlay/align functions to define a constant HOUSE that is the image of a house with a roof and door (and circle if you’re feeling bold enough for a door knob). Be creative!

Exercise 6 Define a constant WINDOW, as the image of a window, and place two of them on your HOUSE, defining HOUSE-WITH-WINDOWS. Note how in using a constant we only have to draw it once and get to use it twice!

The next step is a bit tricky, and will require you to understand a bit about how colors are represented in DrRacket (and other languages!).

Colors in DrRacket can either defined via a name (like "blue" and "red"), or by numbers, representing the amount of red, green, and blue (each a number from 0-255) using the color function...

(color red-val green-val blue-val)

For example, a bright red square could be created as either of the following:

(square 100 "solid" (color 255 0 0))
(square 100 "solid" "red")

Now consider the following function, which uses a mathematical formula to produce a range of blues:

; SIGNATURE HERE
; PURPOSE HERE
(define (sky-color t)
  (color 0 0 (abs (- (remainder t 510) 255))))

This function always uses 0’s for red and green, but differs in the amount of blue. If it helps, here is an infix representation of the equation:

|(t remainder 510) - 255|

and here is a visual depiction of how the amount of blue changes as a function of the value of t: https://www.desmos.com/calculator/ntq43wwjpg

As you can see, the amount of blue moves linearly up and down between 0 and 255.

Exercise 7 Replace "SIGNATURE HERE" above with a signature for the sky-color function. Replace "PURPOSE HERE" with a purpose statement for the sky-color function.

Exercise 8 Now we are ready to put it all together. Define a function scene that uses your HOUSE-WITH-WINDOWS constant, as well as the sky-color function, to visualize a single frame of the movie, where the frame number determines the sky color.

So,

(scene 0)

should have your house in front of a bright blue background, whereas...

(scene 255)

should have your house in front of a black background (night!). The goal is that when you run (animate scene), you will see a movie of your house, with the sky getting darker and then lighter behind it.

Before You Go...

If you have any questions regarding the lab or the course make sure to ask a TA or tutor.