On this page:
Introduction
Using Dr  Racket
Conditions
Programming with Images
Before You Go...
6.6

Lab 1 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: Read and sign the course contract. Find a partner and exchange contact information.

Exercise 1 Read and sign the course contract.

Exercise 2 Exchange contact information with your partner so that you can meet up outside of the lab. The next homework is due Monday!

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

Conditions

Sample Problem Define the function how-hot which consumes a temperature 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.

(define (how-hot temp)
  (cond [(<= temp 45) "cold"]
        [(<= temp 75) "comfortable"]
        [else "hot"]))

Sample Problem Define the function absolute which consumes a number and produces the absolute value of that number.

(define (absolute n)
  (cond [(< n 0) (- n)]
        [else n]))

Exercise 3 Define the function letter-grade. It consumes a score (number) between 0 and 100 and produces the corresponding letter grade ("A", "B", "C", "D", or "F"). You may choose your own grading scale.

Exercise 4 Define the function how-long which consumes a Number representing how long (in minutes) you have to wait for something and produces a String which represents how you feel about waiting this long. If the number is less than or equal to 10 it should produce "great!". If the number is greater than 10 but less than or equal to 60 it should produce "okay". If the number is greater than 60 it should produce "not good".

Exercise 5 Define the function good-weather? which consumes a String representing the current weather and produces a Boolean which is true if the weather is good and false otherwise. You may determine what good weather means to you (for example my function would return true if the string was "sunny" or "blizzard" but some people would argue that a blizzard is not good weather).

Exercise 6 Define a function 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 up to 10 characters, it’s 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

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

(define (red-frame img)
  (overlay img (rectangle (+ 1 (image-width img))
                          (+ 1 (image-height img))
                          "solid" "white")
           (rectangle (+ 4 (image-width img))
                      (+ 4 (image-height img))
                      "solid" "red")))

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 image of the text. You may decide which wishes to accept. You can use images from the web or build the images yourself using circles, squares, and other shapes that you can draw in Dr. Racket.

Exercise 8 Define the function draw-sheep. It accepts a Number, representing the x-coordinate of the sheep and draws a sheep onto a green field at that x coordinate. You may place the sheep at whichever y-coordinate you like.

Exercise 9 Define the function on-top which takes two Images and overlays one on the other such that the tops of the images line up.

Before You Go...

Make sure you exchange contact information with your lab partner, as you will be working together on problem sets and in labs in the future. If you have any questions regarding the lab or the course please feel free to ask a TA or tutor.