Beginning Scheme

Let's begin today's lab by finding out how to get Scheme to execute the first program mentioned in the reading:

(sqrt 137641)

Exercise 1: Interacting with DrScheme

Log in on any MathLAN workstation. Move the mouse pointer onto the red-and-blue-circle icon and click the left mouse button once to start the DrScheme programming environment.

In the Interactions window -- the lower of the two large text areas -- DrScheme displays a one-line greeting, a reminder of which dialect of Scheme it expects to see, and a prompt (in this case, a greater-than sign), indicating that DrScheme is ready to deal with any command or definition that you type in.

Welcome to DrScheme, version 101.
Language: Graphical Full Scheme (MrEd).
>

Move the mouse pointer to the right of the prompt, click the left mouse button, and type in the program shown above. (If you prefer, you can select it from the Netscape Navigator window by moving the mouse pointer to the beginning of the program, pressing and holding the left mouse button, dragging the mouse pointer to the end of the program, and releasing the left mouse button. The background color against which the text that you have selected changes during this process, so that you can see the boundaries of the selection clearly. You can then paste the selected text into the interactions window by moving the mouse pointer to the right of the prompt and clicking the middle mouse button.)

Press the <Enter> key after the right parenthesis. At this point, DrScheme examines your command, translates it into a sequence of instructions for the computer's central processor, feeds those instructions to the processor, and displays the result. Because this particular program is extremely simple, the result is printed immediately. When the processing is complete, the interactions window should look like this:

> (sqrt 137641)
371
>

Exercise 2: Checking the computer's work

Confirm, by pencil-and-paper computation or with the help of a pocket calculator, that 137641 is indeed the square of 371. If it works out, we'll consider this program a success.

Exercise 3: A simple arithmetic expression

Tell DrScheme to multiply 162 by 1383.

Exercise 4: The abs procedure

The Scheme procedure abs computes the absolute value of its argument. Have DrScheme compute the absolute value of -197.

Exercise 5: The expt procedure

The Scheme procedure for raising a number to some power is expt. Call this procedure to compute the cube of 19 (that is, the result of raising 19 to the power 3), and again to compute the nineteenth power of 3. What does this indicate about the relationship between procedures and argments in Scheme?

Exercise 6: Goofing around with the syntax of arithmetic expressions

It is harmless, though unproductive, to try to give DrScheme arithmetic expressions that follow the more usual algebraic convention in which the operator is written between the operands. Try it: Type each of the following expressions at the prompt in the Interactions window and see what reaction you get.

  1. (2 + 3)
  2. 7 * 9
  3. sqrt(49)

If you're surprised or puzzled by the results, take a look at the explanations.

Exercise 7: A simple definition

Write a definition that will cause Scheme to recognize dozen as a name for the number 12. Type it into the Interactions window. How can you tell whether DrScheme has learned this name?

Exercise 8: Renaming a procedure

Write a definition that will cause Scheme to recognize raise-to-power as an alias for expt. How can you tell whether DrScheme has learned the new name?

Exercise 9: The Definitions window

The upper text area in the DrScheme window, which is called the Definitions window, is used when you want to prepare a program ``off-line,'' that is, without immediately executing each step. Instead of processing what you type line by line, DrScheme waits for you to click on the button labelled Execute (the second button from the right, in the row just below the menu bar) before starting to execute the program in the definitions window. If you never click on that button, fine -- your program is never executed.

As its name implies, the definitions window usually contains definitions rather than commands, although either kind of expression can be written in either window. The difference is simply that we generally want an immediate response to a command, whereas definitions are usually processed in bulk.

Warning: When you click on the Execute button, the contents of the interactions window are erased. The idea is that executing the program in the definitions window may invalidate the results of previous interactions. Erasing the results that may now be inconsistent with the new definitions ensures that all visible interactions use the same vocabulary. This is actually a helpful feature of DrScheme, but it can take you by surprise the first time you see it happen. Just make sure that you have everything you need from the interactions window before clicking on Execute.

Copy the definitions you wrote for exercises 7 and 8 into the definitions window and execute them.

Exercise 10: Using learned definitions

After doing exercise 9, move the mouse pointer into the Interactions window, click the left mouse button, and type in the expression

(multiply 5 dozen)

Explain the result.

Exercise 11: Procedure calls in the Definitions window

It is permissible to add procedure calls to a program that you're developing in the Definitions window. DrScheme waits to evaluate them until you press the Execute button, at which point DrScheme processes everything in the Definitions window -- definitions and commands alike. The results are displayed in the Interactions window, however.

Add the procedure call

(* dozen dozen)

at the end of the program in the Definitions window and execute it. Explain the result.

The conventional practice is to use the Definitions window to develop programs and the Interactions window to test them and experiment with them.

Exercise 12: Saving a program

You can save the contents of either window in a file at any time by selecting Save Definitions As Text or Save Interactions As Text from the File menu. DrScheme pops up a window in which you can specify the name of the file:

(screenshot)

In this window, you can the contents of the white text field near the bottom of the pop-up window, adding a slash and the name of the file. When you move the mouse pointer onto the OK button and click the left mouse button, DrScheme saves the program and removes the pop-up window.

Save the definitions that you copied into the Definitions window in exercise 9 in a file named beginning-Scheme.ss. (Conventionally, the names of files containing Scheme programs end in .ss or .scm.)

You'll probably save the Definitions window much more often than the Interactions window, because it's often convenient to pick up a saved program and extend it by adding more definitions and commands, or even to improve on some of the existing ones. A saved Interaction window is a transcript of tests and experiments, something more like a historical record -- you might want to look at it again sometime, but it makes no sense to take it up and change it.

Exercise 13

Play with the insertion, deletion, and cursor-movement operations in the DrScheme Definitions window for a little while, until you feel comfortable with them.

Exercise 14

Use the Cut operation to delete everything you've typed, leaving the definitions window empty.

Exercise 15

Type the following Scheme program into the definitions window.

(define area 121)
(define square-root sqrt)
(define side (square-root area))

The last one is a definition with a procedure call inside it; the effect is to have DrScheme compute the square root of 121 and define side as a name for the result.

As you are typing, note that when you type a right parenthesis, DrScheme momentarily moves the cursor back to the matching left parenthesis and grays out the text they enclose. Since longer Scheme definitions often contain many pairs of nested parentheses, this graphic convention makes it much easier to edit Scheme programs.

Exercise 16

Save the program in the definitions window in a file named area-and-side.ss.

Exercise 17

Have DrScheme compute the result of dividing 103212 by 732. Then have it divide the same dividend by 564. (Use <Esc> <P> to copy the preceding command and edit it.)

Exercise 18

Exit from DrScheme and restart it. Then invoke the load procedure to have DrScheme learn the definitions that you saved in area-and-side.ss (in exercise 4). You won't see any immediate response in the Interactions window, because the file has only definitions in it.

Exercise 19

Confirm that DrScheme has memorized the definitions in area-and-side.ss by giving it the following commands, which depend on those definitions:

(* area 4)
(square-root 81)
(* side area)

If you get sensible responses in the last step (presumably 484, 9, and 1331, respectively), DrScheme read the definitions correctly. If DrScheme had not seen these definitions, you would have received advisory messages looking like this:

> (* area 4)
reference to undefined identifier: area
> (square-root 81)
reference to undefined identifier: square-root
> (* side area)
reference to undefined identifier: side

This is how DrScheme reports that it doesn't know the meaning of a name that you've tried to use.

Exercise 20

Have DrScheme save the program in the Interactions window in a file named area-and-side.interactions.

Exercise 21

Add appropriate comments to the area-and-side program in your Definitions window and save it again.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~gum/courses/151/labs/beginning-Scheme.xhtml

created July 26, 2001
last revised July 26, 2001

John David Stone (stone@cs.grinnell.edu)

Ben Gum (gum@cs.grinnell.edu)