(sqrt 137641)
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 208. Language: Standard (R5RS). >
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 Firefox 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:
Welcome to DrScheme, version 208. Language: Standard (R5RS). > (sqrt 137641) 371 >
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.
Tell DrScheme to multiply 162 by 1383.
abs procedure
The Scheme procedure abs computes the absolute value of its
argument. Have DrScheme compute the absolute value of -197.
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 arguments in Scheme?
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.
(2 + 3)7 * 9sqrt(49)If you're surprised or puzzled by the results, take a look at the explanations.
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?
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?
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 Run (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 theRun 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 Run.
Copy the definitions you wrote for exercises 7 and 8 into the definitions window and execute them.
After doing exercise 9, move the pointer into the Interactions window, click the left mouse button, and type in the expression
(raise-to-power dozen 3)
Explain the result.
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 Run 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.
You can save the contents of either window at any time by selecting the
appropriate item from the File menu. In this course, you will
always want to save your programs as texts. They will be kept in files, in
a folder on MathLAN (called your home directory that only you
have access to. You will need to think of a file name for each program
that you save; conventionally, the names of files containing Scheme
programs end in .ss.
To save the contents of the Definitions window in a text file, move the
pointer onto the word File at the left end of the DrScheme menu bar
and left-click. Move the pointer onto the item Save Other on
the menu that appears and left-click again; another menu will appear. Move
the pointer onto the item Save Definitions As Text... and left-click
again.
At this point, DrScheme pops up a window entitled Save, in which you
can specify the name of the file, by typing it into the white rectangular
text field. There will be some text already there -- the word /home/, followed by your user name; that's the name of your home
directory. Type in a slash (a forwards slash, not a backwards one -- the
key is just above the 〈Enter〉 key) to separate your user name from
the name of the file you want to create, then type in the name of the file.
Here's an example of what should appear in the white rectangle at that
point:
/home/spelvin/square-root-example.ss
In this example, the username is spelvin and the name that the user
chose for the file is square-root-example.ss.
File names can contain letters, digits, hyphens, dots, and underscores; theoretically, it's also possible for them to contain spaces and various other punctuation characters, but some of the GNU programs behave oddly when dealing with files whose names have more exotic characters in them, so it's best to stick with the readable ones.
When the file name looks right, move the pointer onto the OK
button at the lower right corner of the Save window and left-click.
DrScheme saves the program and removes the pop-up window.
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.
When you have changed the contents of the Definitions window without saving
the changes, a Save button appears on the tool bar near the top of
the DrScheme window. Left-clicking on the Save button updates the
file in which you have saved the program, so that it includes your most
recent changes. If you spend more than a few minutes working on a program,
it's generally a good idea to click that button from time to time in order
to make sure that the text file actually includes all of your changes.
Note, however, that for the first save, you should go through the
business with the file menu in order to be sure that the Definitions window
is saved as a text file.
Save the definitions that you copied into the Definitions window in
exercise 9 in a file named beginning-Scheme.ss.