Although it is theoretically possible to write long Scheme programs by running Scheme in interactive mode and typing in the constituent definitions and expressions, it's more usual to develop them ``off-line'' using a text editor -- in our case, XEmacs.
One of the traditional hazards for novice Scheme programmers is keeping track of the parentheses in a deeply nested expression and making sure that they are matched up correctly. XEmacs simplifies this process by automating it. Every time you type a right parenthesis in Scheme mode, XEmacs inserts it and then briefly highlights the corresponding left parenthesis (without changing the point at which subsequent characters will be inserted). By watching the highlighting as you type in right parentheses, you can ensure that each of them matches the appropriate left parenthesis, or make a correction if any of them does not.
In Scheme mode, pressing the <Tab> key automatically indents the line containing the editing cursor to the correct position. If you use XEmacs to edit Scheme code and follow every carriage return with a tab, you'll always have correctly indented Scheme code.
To transfer individual definitions and expressions from an XEmacs editing window to a Chez Scheme interactive interface running in a visible hpterm window, you can cut and paste: Move the mouse pointer to the beginning of the definition or expression, press and hold the left mouse button, drag to the end of the definition or expression, release the left mouse button, move the mouse pointer into the window where Chez Scheme is running, and click on the middle mouse button. The selected text will be submitted to Chez Scheme, just as if you had typed it in from the keyboard.
Once a file is saved, however, there are simpler ways of getting Chez
Scheme to read it. One way is to use the load procedure
discussed in the previous lab; another is to name the file on the command
line that starts up Chez Scheme:
bourbaki% scheme frogs.ss Chez Scheme Version 5.0c Copyright (c) 1994 Cadence Research Systems >
Chez Scheme reads and processes the program in frogs.ss before going into interactive mode and issuing the prompt. This is handy if you have a library of definitions that you want to load in before beginning to interact with Chez Scheme. Any number of files may be named on the command line in this way.
A typical cycle in the development of a program is discovering an error by
experimentation in the interactive interface, fixing it in XEmacs by
revising a definition in a Scheme program file, saving the file from
XEmacs, reloading it with the load procedure, and resuming
testing. It would not be unusual to have both the hpterm window
running Chez Scheme and the XEmacs window containing a Scheme program open,
side by side, throughout a programming session.
Files containing Chez Scheme source code conventionally end in .ss (``Scheme source''). The Scheme mode supplied with XEmacs does not recognize this convention (it assumes .scm instead), but a Chez Scheme programmer can make XEmacs aware of it by revising the XEmacs configuration file, .emacs, in her home directory.
The XEmacs configuration file is actually a short computer program that is executed while XEmacs is starting up, before the window appears. (In fact, it happens to be written in a language that is a close relative of Scheme, XEmacs Lisp. We won't linger to examine the similarities and differences, though -- the current objective is simply to customize XEmacs to work with Chez Scheme programs.)
Revise your XEmacs configuration file, as follows:
Start XEmacs and open the file .emacs in your home directory.
Position the editing cursor at the bottom of the file by moving the mouse pointer to the end of the file and clicking with the left mouse button.
The customizations that you need to add are stored in the file xemacs-customization. To copy this file into .emacs, bring up the File menu from the menu bar and select the Insert File... operation, then type in the full name of the file in which the customizations are stored. The contents of the file should appear in the XEmacs window.
Save the .emacs file and exit from XEmacs.
Next, let's check whether the customization succeeded. Restart XEmacs and open a new file named sample.ss in the XEmacs window. Look at the ``mode line'' at the bottom of the XEmacs window; it tells you what file you're currently editing and which of XEmacs's numerous editing modes are active. If you see the word Scheme someone on that line, you did step 1 correctly.
Type in the following Scheme definition:
(define h5 (+ 1/1 1/2 1/3 1/4 1/5))
Place the editing cursor anywhere on the second line of the definition and press the <Tab> key. Notice that XEmacs indents the line. The number of spaces that XEmacs inserts is determined by a set of conventions that have been found to result in highly readable programs. (The textbook follows the same conventions -- probably because it was written with the help of XEmacs.)
The customization that you performed in step 1 also enables you to run Chez Scheme, if you prefer, inside the XEmacs window rather than in the hpterm window. The choice between using hpterm or XEmacs as the environment in which to run Chez Scheme is a matter of personal preference and style; after today's lab, you may use whichever setup you find most comfortable.
Today, however, we'll run Chez Scheme inside XEmacs, to see what it looks like. Press the <F1> key. Your XEmacs window will split into two parts, and the Chez Scheme interactive interface will be started in one subwindow.
Since XEmacs still wants to provide full-window editing, it does not transmit definitions and expressions to the Chez Scheme interactive interface when a line break is inserted (as for instance when the programmer presses the <Enter> key that is just above the right-hand <Shift> key, on the typewriter part of the HP keyboard. Instead, XEmacs requires the programmer to signal explicitly that she has finished editing a definition or an expression and wants Chez Scheme to deal with it. The other <Enter> key, the one in the lower right-hand corner of the numeric keypad, is used for this purpose. When it is pressed, XEmacs collects all of the Scheme definition or expression that immediately precedes the editing cursor and transmits it to Chez Scheme for processing -- no matter which subwindow the editing cursor is in. Chez Scheme always displays its response in the subwindow where it is running.
Move the editing cursor to the end of the definition that you typed in back in step 3 and press the keypad <Enter> key. Notice that Chez Scheme issues a second prompt, right next to the first one; this indicates that the interactive interface has processed the definition and is now ready for another definition or command.
Move the editing cursor to the position immediately following this
second prompt, type h5, move the cursor to the beginning of the
next line, and press the keypad <Enter> key. Notice that Chez Scheme
responds by printing the value of the symbol h5, proving that
it did indeed see the definition in step 6.
Of course, it is also possible to cut and paste between subwindows within XEmacs, and some programmers will find that the contents of the Scheme buffer are easier to read if they use this technique every time they want to submit something to Chez Scheme. But it is often convenient to move the editing cursor to the correct position in a handy subwindow and then press the keypad <Enter> key to load that definition into Chez Scheme.
When Chez Scheme is running in an XEmacs window, you can't shut it down with <Control/D>, since XEmacs regards this as a delete-character command. Type (exit) instead. Although this has the syntax of a procedure call, Chez Scheme recognizes it as a shutdown directive.
If you forget to exit from Chez Scheme before closing the XEmacs window in which it is running, XEmacs will remind you by printing the question
Active processes exist; kill them and exit anyway? (yes or no)
at the bottom of the window. If you type the word yes in response, XEmacs will shut down Chez Scheme for you as it shuts itself down.
The author of a computer program has to accommodate both the computer that executes the program and the human beings who read it. If the program code is correct, the computer very seldom has any difficulty executing it correctly; however, it may still be difficult for human readers to understand what the program is supposed to do and how it is supposed to do it. So the author needs to be able to include in the program explanations directed to the human reader. The computer can simply ignore these explanations, but they are nevertheless an essential part of the program, because communicating with human readers is an essential part of the programmer's job.
Scheme therefore allows the programmer to write comments into the text of a program. Comments are ignored completely by the interactive interface and have no effect on the execution of a program; they are intended solely for human readers. A Scheme comment begins with a semicolon and extends to the end of a line. A multi-line comment can be constructed by starting each of the lines with a semicolon. For instance, here is a comment from the beginning of a Scheme program:
;;; This Scheme program reads in, from a file named RSCyg, two columns of ;;; data, representing measurements of two observable quantities at various ;;; times. It computes and displays the coefficient of correlation ;;; computed from the data. ;;; John Stone -- February 17--20, 1995; July 21, 1995; June 28, 1996; ;;; June 30, 1996.
Although one semicolon is enough to begin a comment, most Scheme programmers have the habit of using the number of repetitions of the semicolon character to indicate the scope or importance of a comment. A three-semicolon comment line is more likely to apply to the program as a whole, or to be extremely important; a similar line beginning with just one semicolon is likely to apply only to the line that immediately precedes or follows it and to be an off-hand remark.
Add a comment to sample.ss in which you give your name, the
date, and a description of why you wrote the program stored in that file.
Save the file and use the load procedure to load it into the
Scheme interactive interpreter again.
When you give the name of a file to Scheme's load procedure, you
enclose it in double quotation marks:
(load "sample.ss")
Scheme treats a sequence of characters enclosed in double quotation marks
as a string. Just as Scheme provides arithmetic procedures like
sqrt and + for operating on numbers, it
provides string procedures (with names like string-length and
string-append) for operating on strings as data values. You
can even define symbols to stand for strings:
(define greeting "Hello there!")
Submit this definition to the Scheme interactive interpreter. Then ask
it to evaluate (a) the symbol greeting; (b) the expression
(string-length greeting); (c) the expression
(string-append "Hey!" greeting).
This document is available on the World Wide Web as
http://www.math.grin.edu/courses/Scheme/Scheme-within-XEmacs.html