CS 3 (Garcia) Spring 2001 Lab assignment 1


Goals

This lab is intended to introduce you to the basics of using the Gambit Scheme programming environment.

Preparation

Read chapter 1 in Simply Scheme, and bring an initialized floppy disk to your lab section. If your lab is in Etcheverry, bring a Zip disk, and whenever you read "floppy" in this lab, think "Zip".

Also become familiar, if you are not already, with the following Macintosh operations:

a. initializing a floppy disk;

b. finding a file on the hard disk or the floppy disk;

c. selecting and opening an item on the Macintosh desk top;

d. double-clicking with the mouse;

e. selecting a command from a menu with the mouse.

For students who are not familiar with these operations, the t.a.s will run a short workshop at the start of the first week's lab sections. (If you'd like to participate in this workshop, sit near the front of the room.)

General lab procedure

In general, each week's lab assignment will consist of exercises that, once completed, will be "checked off" by the lab t.a. You will earn up to 4 checkoff points each week in lab, and up to 40 checkoff points during the semester. Except in case of dire circumstances affecting the entire lab section, you will not have the opportunity to earn the checkoff points for labs you do not attend.

Your lab work in subsequent labs will generally be done with a partner, one of the other students in your lab section; one of the tasks in this week's lab is to acquire a partner and report your names to the lab t.a.

Activity 1: Entering and leaving the Gambit programming environment

First, login on a computer in 175 LeConte or 3116 Etcheverry. Where it asks for your username, type cs3. The password is (cs3) .

(Note: This locations are a little different in the Etcheverry labs; when it does differ, we'll label what is different with (Etch)). Once you are logged in, double-click on the MOCHA.PROGRAMS (Etch: Mac HD). Then double-click on the folder labeled Classes, then double-click on the folder labeled CS3, and finally double-click on the folder labeled "UCB Scheme" (also known as the "Gambit" or "MacGambit" folder. Whew, that's a lot of double-clicking. Every file you will need from now on will be located in the MOCHA.PROGRAMS / Classes / CS3 folder or one of its subfolders (The UCB Scheme folder, for example, is a subfolder of the CS3 folder). From now on, any time I refer to a folder, you may assume that it is a subfolder of the CS 3 folder. For example, when I say that a copy of this lab assignment is in the Labs folder, I mean that you can reach it by going to MOCHA.PROGRAMS (Etch: Mac HD), then Classes, then to CS3, and finally to Labs.


Locate Gambit on the hard disk. Its icon appears on the left in the figure below. It could also be named "MacGambit Interpreter"


Gambit's icon


Gambit's icon after it's selected

There are two ways to start MacGambit. One way is to click on the MacGambit icon, which will change its appearance as displayed on the right in the figure above. You would then choose Open from the File menu:


Another way is to double-click on the MacGambit icon. Either way will, after a bit, present you with a screen that looks something like the following (but the "v2.2.matt+brian" should say "v2.2.ucb2"):

Start Gambit now using one of these techniques.

There are several ways to leave MacGambit. One is to choose the Quit command from the File menu. (Simply Scheme describes another way.) Leave Gambit now, and start it again.

Activity 2: Moving and resizing windows

The Interaction window is the window in which you will "speak" directly to the Gambit Scheme interpreter. The conversation will start shortly. First, experiment with moving and resizing the window.

Click first on the "resize" box in the bottom right-hand corner of the window, and, while holding the mouse button down, move the mouse. This should make the window smaller or larger, depending on which way you move the mouse. Then click once on the "zoom" box at the upper right-hand corner of the window. Watch what happens. Then click on it again. Finally, click on the "title bar" of the window and, while holding the mouse button down, move the mouse. The whole window should move.

Now click somewhere outside the Interaction window. This should bring you to the Macintosh Finder. You may return to Gambit either by clicking again on the Interaction window if it is visible, or by selecting MacGambit from the menu at the upper right-hand corner of the screen.


Do this now.

Activity 3: Conversing with the Gambit interpreter

Here you will start your "conversation" with Gambit. If the blinking cursor is not immediately after the "prompt"-the last colon in the Interaction window-click to the right of the prompt to move the cursor there. The cursor indicates where text will appear when you start to type.

First experiment with typing nonsense to Gambit. Type

x

then hit the Enter key (not the Return key). The following should appear:

MacGambit (v2.2.matt+brian)


: x

*** ERROR -- Unbound variable: x


1:

Note that what you type appears onscreen in boldface. Note also that the prompt has changed to a 1 followed by a colon. This means that you've made an error. The "unbound variable" message is a common one in Scheme programming; it essentially means that Gambit doesn't know what "x" is. Every time you make an error, the number increases; type

y

and Enter to verify this. Typing t-that is, holding down the and t keys simultaneously-or selecting Toplevel from the Command menu resets the number and gets you back the colon prompt; you don't need to do this, but programmers generally do after an error to erase the evidence of their fallibility.

Incidentally, the Enter key is how you tell Gambit that you're done speaking and ask it to "interpret" what you said. It's somewhat like saying "over" when you talk on CB radio. On most keyboards, there is also a Return key; it has no special meaning for Gambit. is similar to a shift key. Instead of changing from lower case to upper case, though, it changes from "regular character" to "command" and in fact is sometimes called the "command key".

Now type some meaningful things to Gambit, for instance the lines on page 5 of Simply Scheme. Don't forget to type Enter at the end of each line. This should result in the following:

: 6

6


: (+ 4 7)

11


: (- 23 5)

18


: (+ 5 6 7 8)

26


:

Observe as you type each right parenthesis that the corresponding left parenthesis blinks momentarily. Get in the habit of watching for this; it's quite helpful for keeping your parentheses organized.

Activity 4: Working in an editor window

You are now going to work with another window, called an "editor window". You can think of editor windows as both your notebook and your scratch pages. You can type whatever you like into an editor window and save it into a file. This window provides the easiest means of editing functions and other interesting things you might create. Gambit will ignore what you write in this window, but you can move things from the Editor window into the Interaction window whenever you want.

To create an editor window, either choose New from the File menu or type n. Do this now. A new window titled "Untitled" should appear on your screen. Resize and move it until you can see both the Untitled window and the left side of the Interaction window on the screen, as shown below.


Type the following lines into the Untitled window, typing Return-not Enter-after each line. Note that three of the lines are blank.

(word 'comp 'uter)


(define (acronym phrase)

(accumulate word (every first phrase)))


(acronym '(american civil liberties union))


(acronym '(university of california at berkeley))

Observe that nothing has happened in the Interaction window.

The Enter key does pretty much the same thing in an editor window as it does in the Interaction window, that is, tells Gambit what you typed immediately to the left of the cursor. Test this in two ways.

- First move the cursor to the end of the first line by clicking on the line somewhere to the right of the right parenthesis, then hit Enter. You should see Gambit respond in the Interaction window with "computer" and another prompt.

- Now move the cursor to the end of the last line, and hit Enter again. This should produce an error message indicating that Gambit doesn't know what "acronym" is.

As mentioned in Simply Scheme, the line containing the word "define" is what is used to tell Gambit what "acronym" should mean, but apparently this hasn't yet been communicated to Gambit. That's because the Enter key as just used only communicates single things from an editor window to Gambit.

To communicate multiple things to Gambit, you must use the Macintosh selection facility. Clicking somewhere in the window and "dragging"-moving the mouse while holding down its button-highlights and selects the corresponding text. A shortcut for selecting all the text in an editor window is to select the Select All command from the Edit menu or to type s. Do this now, then type Enter. Gambit should respond to each line in turn:

computer


: acronym


: aclu


: uocab


:

A third way to get information to Gambit from an editor window is to copy it to the "clipboard" using the Copy command (in the Edit menu), then to click in the Interaction window and "paste" it after the prompt, using the Paste command. The effect is the same as if you had typed the text in the Interaction window directly.

Activity 5: Saving and loading editor window contents

Another way to "say" a lot of things to Gambit all at once is to create a file, then load it. You'll now practice this.

First change all the occurrences of "acronym" to "acron". The Search menu provides commands for doing this. The Find command allows you to specify a sequence of characters to look for-"acronym" in this situation-and a sequence to replace it with. The Replace All command does the replacement.

Then you need to save the contents of the editor window in a file. Choose Save from the File menu or type s. Something like the following should appear:


The Macintosh file system is asking you where the file should be saved, and guessing that you want to save it on the hard disk where Gambit is. You don't want to do this. Insert your floppy disk. If the disk has already been initialized, you should see the following.


Observe that the floppy disk's name-"shuttle" in the above figure-and icon, something resembling a floppy disk, replace those listed. You should always save files to your floppy disks rather than to the hard disk, since the hard disks are cleaned out every day.

Now type the characters "lab 1.scm" and hit Return or click the Save button. This saves the contents of the editor window in a file named "lab 1.scm". The characters ".scm" at the end of the file name are used to remind yourself that the file contains Scheme code. Observe after you do this that the name of the editor window has changed from "Untitled" to "lab 1.scm" preceded by the name of your floppy disk. (You may use the Open command in the File menu to access and edit code in files that you've previously saved. )

Get back to the Interaction window by clicking in it. Choose the Load command from the Special menu. Select the file named "lab 1.scm" and hit Return or click the Open button. Gambit silently interprets the contents of the file. Now type the following after the prompt:

(acron '(quod erat demonstrandum))

Gambit should respond with

qed

=> Creating this file and loading anesting its contents earns you the first of the checkoff points for this lab assignment.

You may occasionally find it useful to save the contents of the Interaction window, to keep a record of tests of code you have written for reference or homework submission. The Save command works for that too. To avoid confusion, don't put ".scm" at the end of the file's name (the Interaction window contains prompts, error messages, and other things that will not be understood as Scheme code).

Activity 6: Loading a file we've supplied

Occasionally we will provide files for you to use for lab or homework. One such file, containing the code appearing on page 12 of Simply Scheme, is called choices.scm and is in the folder CS 3 code. Choose the Load command from the Special menu (or type j), and navigate through the file structure to find it. More information on how to access the file will be available at your lab section. Eventually you will get to the file itself; clicking Open will load its function definitions into Gambit. Test it by asking for choices of menus different from those in Simply Scheme.

=> Loading this file and testing the choices function earns you the second of the checkoff points for this lab assignment.

Activity 7: Printing the contents of a window

You may send the contents of any window to one of the printers in 310 Davis by clicking in the window, then selecting the Print command from the File menu. Send the contents of the Interaction window to the printer in this way. You may use the printed output to help verify to your t.a. that you have tested the acronym and choices functions as required for checkoff.

=> Printing the contents of the Interaction window earns you the third of the checkoff points for this lab assignment.

Activity 8: Finding a partner

Find a lab partner among the other students in your lab section. Tell your lab t.a. your name and your partner's name.

=> Forming a partnership and reporting it to your lab t.a. earns you the fourth of the checkoff points for this lab assignment.

Activity 9 (optional): Starting on homework assignment 1

If you have time left over, you may wish to load and experiment with the functions function as described in Chapter 2 of Simply Scheme. You will work with this function for homework assignment 1.

Commands, techniques, etc. covered in this assignment

- starting and leaving Gambit

- moving and resizing windows

- "conversing" with Gambit in the Interaction window

- creating an editor window, typing and modifying text in it, & sending it to Gambit

- saving and loading files of Scheme code

- printing the contents of a window

Summary of checkoff requirements

1. (Activity 5) Create a file called "lab 1.scm" on a floppy disk by saving an editor window, then load and test the function whose definition appears in the file.

2. (Activity 6) Load the file called "choices.scm" that we've provided, and test the choices function whose definition appears in the file.

3. (Activity 7) Print the contents of the Interaction window.

4. (Activity 8) Form a partnership, and report it to your lab t.a.


5