Scheme within XEmacs

The advantages of editing

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 dtterm 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 dtterm window running Chez Scheme and the XEmacs window containing a Scheme program open, side by side, throughout a programming session.

Customizing XEmacs for Chez Scheme programming

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 revision need only be made once. Afterwards, XEmacs will automatically be reminded of the .ss convention every time it is started up.

The XEmacs configuration file is actually a short computer program that is executed while XEmacs is starting up, before the window appears. (In fact, this program happens to be written in a language that is a close relative of Scheme -- Emacs 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.)


Exercise 1

Revise your XEmacs configuration file, as follows:


Exercise 2

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 somewhere on that line, you did exercise 1 correctly.


Exercise 3

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 or a closely related text editor.)


Running Chez Scheme in an XEmacs window

The customization that you performed in exercise 1 also enables you to run Chez Scheme, if you prefer, inside the XEmacs window rather than in the dtterm window. The choice between using dtterm 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.


Exercise 4

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.


Exercise 5

Move the editing cursor to the end of the definition that you typed in during exercise 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.


Exercise 6

Move the editing cursor to the end of the line containing the two prompts 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 exercise 4.

If you followed directions carefully, the Scheme interaction window now looks like this:

This is the Scheme process buffer.
Type C-x C-e to evaluate the expression before point.
Type C-c C-c to abort evaluation.
Type C-h m for more information.

Chez Scheme Version 5.0c
Copyright (c) 1994 Cadence Research Systems

> > h5
137/60
> 

If you skipped a step, it may look like this instead:

This is the Scheme process buffer.
Type C-x C-e to evaluate the expression before point.
Type C-c C-c to abort evaluation.
Type C-h m for more information.

Chez Scheme Version 5.0c
Copyright (c) 1994 Cadence Research Systems

> > h5137/60
> 

The step you skipped was moving the cursor to the beginning of the next line before pressing the keypad <Enter> key. Chez Scheme printed the value of h5 immediately, right up next to the expression you typed. This is confusing. So it's a good idea to form the habit of pressing the keypad <Enter> key only at the beginning of the line below the end of the expression you want to evaluate.


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 <Ctrl/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)

in a pop-up box superimposed on it. If you click on the small rectangle containing the word Yes in response, XEmacs will shut down Chez Scheme for you as it shuts itself down.

Comments in Scheme

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
;;; derived from the data.

;;; This program was written as part of a comparative series in which the
;;; same algorithm was implemented in HP Pascal, C, C++, Java, and Scheme,
;;; to provide the basis for a discussion of the advantages and
;;; disadvantages of each of these languages for expressing elementary
;;; algorithms.

;;; John David Stone
;;; Department of Mathematics and Computer Science
;;; Grinnell College
;;; stone@math.grin.edu

;;; Created February 17--20, 1995
;;; Revised July 21, 1995: The READ-DATASETS procedure now returns a pair
;;;   of lists rather than an alternating list.
;;; Revised June 28, 1996: The SQUARED-DISPARITY and PRODUCT-OF-DISPARITIES
;;;   are now local to the procedures that invoke them.
;;; Revised June 30, 1996: An error is now reported when RSCyg is empty.

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 is typically just an off-hand remark.


Exercise 7

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 Chez Scheme is running in an XEmacs window, pressing the keypad <Enter> key does the wrong thing when the previous line ends in a comment. XEmacs starts searching back through the text of the comment, looking for a Scheme expression to evaluate; usually it finds the last word of the comment and mistakes it for a Scheme expression, much to its confusion. In practice, this seldom causes trouble, because comments in Scheme are usually placed above the definition or the expression that is commented on; but if you adopt a different convention and write a comment at the end of the last line of a definition, you may get odd results when you press <Enter>.

String literals

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!")

Exercise 8

Submit the definition of greeting 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).


Symbols and quotation

String literals should not be confused with symbols, which are never enclosed in double quotation marks, are not thought of as being divisible into separate characters, and constitute a completely separate type of data. Symbols are used primarily as names -- for example, in any definition, what follows the keyword define must be a symbol. But regardless of whether it has been defined, a symbol can be used as a datum -- a value that can be operated on, and displayed. Section 1.3 in the textbook contains several examples in which symbols are used as data.

To distinguish symbols used as names from symbols used as data, Scheme requires the programmer to mark the latter by placing a single quotation mark just before the symbol, thus: 'sample-symbol. Without the single quotation mark, Scheme would regard this symbol as a name and attempt to recover its value; unless the name had been previously defined, this attempt would fail, and Scheme would report an error. With the quotation mark, Scheme makes no attempt to evaluate the symbol, but instead treats it as a simple datum.


Exercise 9

Give Scheme a definition that will cause it to recognize gross as a name for the number 144.


Exercise 10

Ask Scheme to evaluate the expression gross (with no single quotation mark on the front).


Exercise 11

Ask Scheme to evaluate the expression 'gross (with the single quotation mark).


Additional exercises

If you reach this point in today's lab before the end of the hour, try the following additional exercises:


Exercise 12

Suppose that you have already given Scheme two definitions, one in which the name gee was associated with the number 9.8 (the gravitational acceleration of the earth at sea level, in meters per second squared) that the name time-of-descent with some number that represented the length of time, in seconds, that it took some object to fall from a height. The height, in meters, can be computed as half of the product of gee and the square of time-of-descent. Write a Scheme expression that carries out this computation.

(Incidentally, most Scheme programmers compute the square of a number by multiplying it by itself rather than by calling expt. In theory, you get the same answer either way, but in some implementations of Scheme the multiplication method reaches that answer more quickly.)


Exercise 13

Find out what happens if Scheme is given two conflicting definitions for the same name: Is the second definition rejected? Is it ignored? Does it supersede the first?


Exercise 14

Use Scheme to compute the average (that is, the arithmetic mean) of the squares of the numbers 132, 197, 244, 268, and 292.


Exercise 15

The quadratic equation 9x2 + 24x + 16 = 0 has exactly one solution. Use Scheme to find it. (Reminder: The ``quadratic formula'' says that the solutions to the quadratic equation ax2 + bx + c = 0 are (-b + (b2 - 4ac)1/2)/(2a) and (-b - (b2 - 4ac)1/2)/(2a).)


This document is available on the World Wide Web as

http://www.math.grin.edu/courses/Scheme/spring-1998/Scheme-within-XEmacs.html

created June 6, 1996
last revised June 21, 1998

John David Stone (stone@math.grin.edu)