On this page:
Introduction & Setup
The Documentation
Error Messages
Simplification with Booleans
Defining Your Own Functions
Before You Go...

Lab 1 The Basics


Purpose: The purpose of this lab is to get you set up on the handin server and 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

Introduction & Setup

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 the course contract and sign it on the handin server.

Exercise 3 Fill out the exam conflicts assignment on the handin server. You MUST fill this out even if you don’t have any conflicts with either of the midterm exams. Please check your class schedule before filling it out so that you have the most accurate information possible.

Exercise 4 If you are using your own laptop, download and install DrRacket on your machine. If you are not, count to 10 in your head to keep things fair and balanced.

The Documentation

DrRacket’s beginning student language comes with all kinds of built-in functions that you can use. Some functions you have seen already include +, string-append, and or.

If you are ever looking for a function but you don’t know exactly what it is called or what arguments it takes in, the best place to find this information is in the documentation for BSL. However, in order to make use of this you need to know how to read it.

Here is an example of an entry in the documentation:

documentation for 'max' function

This is an entry for the function max. You can tell it’s a function because it has the word ’procedure’ written in the upper right corner. This is a synonym for a function.

The function takes at least 1 argument, called x. We can see in the second line that x is a ’real’ (a real number). However, it can take any number of arguments after that. We can see this because the second argument (y) is followed by an ellipsis (the three dots). An ellipsis indicates that you can add any number of further arguments (including zero). The second argument and all further arguments must also be real numbers (see the third line where it tells us that y must be a ’real’). Since further arguments don’t have names in the documentation we must assume that all further arguments have to have the same type as y (the last named argument).

What kind of data does the max function produce? In the first line we see that there is an arrow and then the word ’real’. This means that the function produces a real number. The output type of a function is provided after the arrow.

What does the max function do? The documentation says that it "Determines the largest number—aka, the maximum." If we aren’t sure what this means we can refer to the example given. If we give max the arguments 3, 2, 8, 7, 2, 9, and 0 we get back the number 9 because that is the biggest number of the arguments we gave to the function.

Using your newfound knowledge of the documentation, complete the following exercises.

Exercise 5 What is the signature for the remainder function? That is, what kinds of data do you provide to the function, and what kind of data comes out of it?

Exercise 6 How many inputs can the string>? function take in?

Exercise 7 How many outputs can a function produce?

Exercise 8 Define a function divisible-by3? which takes in a Number and returns #t if the given number is divisible by 3.

Please submit your answer to the above exercise on the handin server.

Error Messages

There are three kinds of error messages that you may encounter while using DrRacket:
  • Syntax errors: This is an error in the construction of your code. This is the same kind of error as when you make a sentence where the words are in the wrong order. So for example, the English sentence "I computer science love" has a syntax error because the word "love" should come before the term "computer science". DrRacket can find these errors before you even try to run your code.

  • Run-time errors: This is an error that doesn’t occur until you try to run your code. Usually, it is caused by providing too many or too few arguments to a certain function.

  • Logical errors: These are the trickiest kind of errors, caused by performing the incorrect task while coding. For example, if you are trying to define a function that adds two numbers but instead your function multiplies the two numbers, it would be a logical error. DrRacket cannot provide an error message for logical errors because only you know what the code is supposed to do. This is where check-expect comes in handy!

Exercise 9 For each of the following pieces of code, identify which type of error is present and correct the code to fix the error. Provide two working tests for each function (using check-expect) to make sure that the functions work as expected.
  • ; get-first-char : String -> String
    ; Get the first character of the given string
    (define (get-first-char str) (substring str))
  • ; get-last-char : String -> String
    ; Get the last character of the given string
    (define (get-last-char str) (substring str (string-length str) (string-length str)))
  • ; add-to-double : Number Number -> Number
    ; Add the second number to twice the first
    (define (add-to-double n1 n2) (+ (n1 * 2) n2))

Simplification with Booleans

In general we want our code to be as simple as possible in order to make it easy for other people to read it. The concept of simplifying code comes up a lot when working with Booleans. There are three main Boolean operators: and, or, and not. Take a look at each of these in the documentation before you begin the following exercise.

Exercise 10 Simplify each of the following expressions as much as possible using the Boolean operators and, or, and not.

Defining Your Own Functions

A drug company has realized that they can charge whatever they want for the medicine they are selling. Capitalism, ho! However, the more they charge, the less people will buy them. In a recent experiment the owner has determined a relationship between the price of his drugs and the average amount of customers. At a price of $500, 5000 people will buy the medicine. For each $1 increase in the price, 2 fewer people will buy it. That is, if the owner charges $501, 4998 people will buy the medicine (on average).

Exercise 11 Translate this idea into a mathematical formula. That is, how many people will buy the medicine at a given price? Then, transform this mathematical formula into a function definition. Remember that your function should not output a negative number, since that doesn’t make any sense in the context of this problem (the function max will be helpful to you here). Be sure to give your function a signature and at least two check-expects.

Unfortunately, the increased sales also come at an increased cost (since they have to produce more medicine). The production of the medicine comes at a fixed cost of $100 to the owner, plus a variable cost of $2 for each customer who buys it.

Before You Go...

If you had trouble finishing any of the exercises in the lab or homework, or just feel like you’re struggling with any of the class material, please feel free to come to office hours and talk to a TA or tutor for additional assistance. We love to teach and you will learn. It’s symbiotic!