On this page:
Introduction & Setup
Using Dr  Racket
Programming with Images
8.1

Lab 1 The Basics

lab!

Purpose: The purpose of this lab is to set up various administrative facilities for you, such as access to the Handin server, and to give you some hands-on experience with the BSL programming language and with the DrRacket programming environment. There is no submission for this lab.

Introduction & Setup

Exercise 1 Follow the instructions on this page to set up an account on the Handin server. You will be using this server to submit all your homeworks and quizzes for the semester.

Exercise 2 Read the course contract and sign it on the Handin server.

Exercise 3 Download and install DrRacket on your machine.

Using DrRacket

Goals: Interact with Dr. Racket’s definitions and interactions windows. Create basic expressions and function definitions. Observe different types of errors.

Exercise 4 After you have installed DrRacket (not before!), open it. If this is your first time doing so, make sure you set the correct language:
  • go to the "Language" menu at the top and select "Choose Language",

  • select the "Teaching Languages" radio button, and within that the "Beginning Student" option (the first). Click "OK",

  • click "Run" in the top right. This actually sets the language. It should now say "Language: Beginning Student" above the prompt >.

Exercise 5 In the definitions window, define a function binomial that takes two numbers a and b as input and returns (a+b)2. Play with this function in the interactions window.

Exercise 6 What a cool function binomial is! You have decided you want to keep this function for the rest of your life. Find out how DrRacket allows you to save a file. After saving, close and then re-open DrRacket and see if you can load your binomial function. Do you need to set the language again?

Exercise 7 In your code of the binomial function, you may have computed a+b twice, in order to form the product of these two equal operands. It would be nicer if there was a function that directly computes the square of another number. But what is it called? Maybe square?

Try square. (Spoiler: won’t do it.) Let’s use the documentation to find out. We suspect the square function (if it exists in BSL) is explained somewhere close to where + is defined. Follow the steps shown in the lecture to find the documentation page for +, and then search on that page for the name and the documentation of a function that squares its number argument.

Finally, rewrite binomial using that function.

Exercise 8 Define a function alternative-names that takes two strings as input, interpreted as the first and last name of a person, and returns two ways of writing the full name of the person as a single string, with the word "or" in between, as follows. Say the first and last names are Joseph and Aoun, resp. Then the string returned should be:

Joseph AounorAoun, Joseph

Note the use of apostrophes around the names—these must be part of the string returned.

Exercise 9 In the interactions window, define two string constants MY-FNAME and MY-LNAME with your first and last name, respectively. Then use these as arguments in a call to function alternative-names, to see what the output is.

Create another example in which the "first name" in fact consists of two separate names, such as Mary Louise. Now make up a last name, and try your function with these inputs. Is the output still meaningful?

Exercise 10 Now move the constant definitions from the previous exercise into the definitions window. Also move your tests into the definitions window, i.e. the calls to function alternative-names that you have used in the previous exercise. What happens to these function calls when you now press the Run button? is that what we want?

Exercise 11 Let’s play anarchy for a moment: call your previously defined functions on some arguments that they are not supposed to take, e.g. binomial on a single number, or binomial on some strings. How does DrRacket respond? do the error messages make sense?

And, where would you enter these "anarchist" examples: in the definitions window or in the interactions window?

Exercise 12 Next to the Run button in DrRacket, there is a Step button. I wonder what that button does!

Let’s find out. Type (binomial 3 4) into the interactions window (don’t press Return), and click on Step. What happens?

Programming with Images

Goals: Get practice programming with images.

Exercise 13 In your definitions window, define two constants SQR1 and SQR2 of type image that each depict a solid square of size 30x30 each, of two colors of your choice.

Exercise 14 Now write code that draws a chessboard of size 3x3 squares (you can make it bigger if you want). Your board should consist of alternating squares SQR1 and SQR2.

If you assign to SQR1 and SQR2 the (same) colors "black" and "black", you should get a large black square—of what size?

What colors should you choose to get a "normal" (albeit only 3x3) chessboard? Maybe black and white, but then the white squares have no edge against the white background, and the chessboard looks "open"—any chess pieces would fall off! How can you add edges to the white squares?

Note that you should put the directive that loads BSL’s image library into the definitions window, before the code for your function: (require 2htdp/image).