Whatever you type into the definitions window, and whatever you type after the last prompt in an interaction window, is immediately displayed and becomes part of a Scheme program. As we saw last time, DrScheme executes the program in the interactions window immediately, one command or definition at a time. It executes the program in the definitions window only when the programmer clicks on the Execute button.
You can always extend the program in either window by typing additional definitions and commands at the end. In the definitions window, you can also perform a variety of editing operations to modify the text of the program:
Typing a letter, a digit, or a punctuation mark adds it to the text at the position indicated by the thin, blinking, black bar (the editing cursor).
Pressing the <Backspace> key deletes the character to the left of the editing cursor, or the preceding line break if the editing cursor is at the beginning of a line.
The arrow keys move the editing cursor around within the region occupied by text.
Positioning the pointer within the text region and clicking the left mouse button causes the editing cursor to jump to that point.
To delete a big chunk of text, move the pointer to the beginning of the
chunk, then press and hold the left mouse button while moving the pointer
to the other end of the chunk. The selected chunk will be ``blacked out''
(displayed in white letters against a black background). Then bring up the
Edit menu and select the Cut operation. This
removes the blacked-out text.
If you have deleted a chunk of text with Cut, you can put it
back in somewhere else, if you want to, by positioning the editing cursor
at the insertion point and selecting Paste from the
Edit menu.
If you make a mistake and create a big mess in your window, select
Undo from the Edit menu to put the window back to
the way it was before and try again. You can select Undo
repeatedly to reverse the effects of a sequence of editing steps.
(If you're curious about the rest of the operations listed on the
Edit menu, they are explained in section
2.2.1.2 of the PLT DrScheme Programming Environment Manual.
Still more editing operations are discussed in section
2.2.3, ``Keyboard shortcuts.'')
Play with the insertion, deletion, and cursor-movement operations in the DrScheme definitions window for a little while, until you feel comfortable with them.
Use the Cut operation to delete everything you've typed, leaving the definitions window empty.
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.
Save the program in the definitions window in a file named
area-and-side.ss.
In the interactions window, insertion and deletion operations work only in the region following the last prompt. The idea is that you cannot change the past: You can't go back and un-give commands that you have already given, and that DrScheme has already responded to. The interactions window is supposed to contain an accurate transcript of what has been done and cannot be undone.
However, while you're working on a command -- even a complicated one that extends over several lines -- the editing operations work exactly as they do in the definition window. There is also one extra editing operation: If you press the <Esc> key (in the upper left-hand corner of our keyboards) and then the <P> key (for ``previous''), DrScheme copies the last command or definition that it processed in the interactions window after the prompt, so that you can revise it or repeat it easily.
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.)
Once you have saved a program in a file, you can load it into DrScheme again from that file, in order to execute it, to modify or extend it, or just to read it over again. In fact, there are several different ways to recover the contents of DrScheme files.
The most straightforward method is to select the Open command from
the File menu, and edit the white text field on the window
that appears so that it contains the name of the file in which your program
is stored:
Clicking on the OK button in the lower right-hand corner loads
your program. Depending on whether the definition or interaction window is
free at the time you load the program, DrScheme either places the program
in the appropriate window or creates a new frame-and-window combination and
loads the program into it. In the latter case, DrScheme usually displays
the program in only one text area. You can make the other one visible by
selecting Show Interactions or Show Definitions
(as appropriate) from the Show menu.
If you know before you start DrScheme that you want to work on a saved
program, you can have it load that program as it starts up by typing the
name of the file containing it as part of the drscheme
command, like this:
bourbaki% drscheme days.ss &
If you just want to execute the program in the file, rather than having it
displayed on the screen, you can use a standard Scheme procedure called
load. When invoked, load directs DrScheme to
open a file and quickly read the program that it contains, memorizing each
definition and executing each command. The load procedure
takes one argument, a character string giving the name of the file
in which the program is stored.
The term characters refers generically to letters, digits,
punctuation marks, and such like -- the lowest-level constituents of text.
A character string is a sequence containing any number of
characters, ``strung together'' in a linear order. In Scheme, a character
string is usually written just by placing the character sequence between
double quotation marks. For instance, the string consisting of the three
lower-case letters c, a, and t, in that order, is
written in Scheme as "cat". (Without the quotation marks,
Scheme would incorrectly interpret cat as a symbol like
sqrt or area, and expect it to stand for
something further.)
Here, then, is what a call to the load procedure looks like.
This one expresses the command ``Execute the contents of the file named
frogs.ss!''
(load "frogs.ss")
The parentheses enclose the procedure call and contain the name of the
procedure, load, and the operand, "frogs.ss".
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.
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:
This is how DrScheme reports that it doesn't know the meaning of a name that you've tried to use.
Have DrScheme save the program in the interaction window in a file
named area-and-side.interactions.
Programs are intended to be read both by people and by computers. Because people understand much richer and more flexible notations than computers -- real languages, as opposed to the extremely limited pseudo-languages used to direct the execution of algorithms -- it is essential to be able to include comments in program files. Comments are intended exclusively for human readers; the computer ignores them as if they were so much blank space.
In a Scheme program, any line that begins with one or more semicolons is a comment:
; This is a comment. DrScheme will store it along with the rest of the ; text of a program, but does not even attempt to execute any part of it. ;;; The following definition, however, is read and processed: (define centimeters-in-an-inch 254/100) ;;; And the following expression is evaluated when this program is ;;; executed: (* 36 centimeters-in-an-inch)
It is also possible to place a comment to the right of a definition or command. The semicolon and everything to its right (on the same line) are ignored during execution.
(define centimeters-in-a-foot (* 12 centimeters-in-an-inch)) ; One foot equals twelve inches.
We'll use comments for several purposes in Scheme programs:
Definitions are seldom completely self-explanatory. Usually, you'll want to preface each definition with a comment explaining what you want to define and how you want to define it.
;;; The number of seconds in a day is the product of the number of hours in ;;; a day (twenty-four), the number of minutes in each hour (sixty), and ;;; the number of seconds in each minute (also 60). (define seconds-in-a-day (* 24 60 60))
I recommend that you write the comment before writing the actual definition. Writing the comment helps the programmer articulate and specify the idea that the definition will express. Especially when you're just starting to program, it's useful to separate this stage of clarifying your ideas from the stage in which you have to think about the syntactic structure of the programming-language notation and the idiosyncrasies of Scheme's expression-evaluator.
If the calculation that your program performs is tricky or subtle, or if it depends on some non-obvious condition that is satisfied in one particular case but usually can't be relied on, you should insert a comment to explain what's going on:
(quotient total sample-size) ; The sample size cannot be zero, since
; at least one sample is constructed by
; the BASE-SAMPLE procedure.
Even if you are the only one who will ever read your program, your future self, trying to understand the program, may need a reminder or two. It is astonishing, incidentally, how quickly this sort of detail fades from one's memory. Write it down, even if you don't now expect to forget it.
Since it's so easy to exchange programs over the Internet, programs that you write and send to others will eventually reach people who have never heard of you. It's a good idea to include a comment at the beginning of each program identifying yourself as the author and explaining how to reach you in case the reader has questions about your program:
;;; John David Stone ;;; Department of Mathematics and Computer Science ;;; Grinnell College ;;; stone@cs.grinnell.edu ;;; created October 14, 1997 last revised March 17, 2000
A long program usually begins with a long opening comment that explains the purpose and structure of the program and presents an overview of what is to come. Here's a real-world example:
;;; The Apache server for hypertext documents records information about its ;;; actions in two log files, access_log and error_log. This program ;;; parses those logs and generates a summary of their contents for the ;;; benefit of the webmaster.
With comments, you can and should think of writing a program as rather like writing an essay in which you describe the problem you're trying to solve and your method of solution. The code sections fit into such an essay as exhibits showing the exact, formal algorithms that express your solution.
Add appropriate comments to the area-and-side program in your definitions window and save it again.
DrScheme provides a lot of documentation about Scheme on-line. The usual entry point is the help desk, a browser that knows where to find all the documentation and provides access to it.
To activate the help desk, select Help Desk from the
Help menu. Clicking on the first link in the Help Desk
window brings up a short introductory document, ``How to use Help Desk.''
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~stone/courses/scheme/editing-Scheme-programs.xhtml
created January 13, 1997
last revised March 17, 2000