Lab 1 The Basics
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.
How Labs Operate
Warm-Up: Introduction and Review. Your head TA introduces the Lab topic, reviews major concepts discussed the past week in the class or the textbook, perhaps shows and reviews a piece of code from the lecture or a past homework, and gives an opportunity for questions, answers, and discussions.
Your head TA may also ask you questions on concepts from the lecture.
The point of this component is to get in the mood for the lab, and to give you the ability to see some course concepts reviewed and discussed without the time pressure of the lecture.
The duration of this part in part depends on your participation.
Joint exercises. Participants solve predefined problems; the head TA discusses solutions afterwards, either in bulk at the end, or after certain "milestones" (the exercises may come in groups).
Your head TA may review your solutions to the exercises with you, but grades are not assigned.
Quiz. You have a short amount of time to solve a particular problem related to the Lab individually. You submit your solution via the Handin server. If you participated in the Lab, these quizzes should be no problem.
Note that the quiz does not necessarily appear at the end of the 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 Fill out the Timezone Questionnaire on the Handin server.
Exercise 4 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 5 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 6 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 7 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 8 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 9 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 Aoun’ or ’Aoun, Joseph’
Note the use of apostrophes around the names—
these must be part of the string returned.
Exercise 10 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 11 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 12 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 13 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 14 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 15 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).