Laboratory Exercises For Computer Science 153 Decisions

Simple Equivalence in Scheme

Goals: This laboratory exercise has both a pragmatic and a conceptual segment. The practical part describes some ways to use XEmacs to run Scheme easily and efficiently. The more general part provides practice with Scheme predicates that test the type of an expression or the equivalence of expressions.

Scheme Within XEmacs

As noted in the previous lab, programming in Scheme normally involves the development of code using a text editor -- in our case, XEmacs. This code then is run by Scheme in an interactive mode.

Within XEmacs, programming can be simplified in two ways. First, Scheme can be set up to indent as you type, following the structure of the code. 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. Second, you can run Scheme within an XEmacs window -- thereby avoiding the need to reload code into a dtterm window.

To take advantage of both of these capabilities, one must customize XEmacs to recognize Scheme programs.

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.)

  1. Revise your XEmacs configuration file, as follows:

    1. Start XEmacs and open the file .emacs in your home directory.

    2. 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.

    3. The customizations that you need to add are stored in the file /home/stone/courses/scheme/spring-1998/xemacs-customization. To copy this file into .emacs, bring up the File menu from the menu bar and select the Insert File... operation. Erase the ~/ characters that are supplied automatically and type in the full name of the customization file. Press <Return> at the end of the file name. The contents of the file will appear in the XEmacs window.

    4. Save the .emacs file and exit from XEmacs.

  2. Next, check whether the customization succeeded. Restart XEmacs and open a new file named 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.

  3. Type in the following Scheme definition:

    (define h5
    (+ 1/1 1/2 1/3 1/4 1/5))
  4. 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.)

Running Chez Scheme in an XEmacs window

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 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.

  1. 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.

  2. 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.

  3. 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.

Comments in Scheme

As you know from your past experience, programs accomplish at least two tasks. First, computers can interpret program syntax and execute the instructions specified. Second, human beings can read the code to understand what it does and how it does it. To accomplish this second task, a programmer should include in the program explanations directed to the human reader. (These explanations also can help the programmer clarify what she or he is thinking.) While the computer ignores these explanations, they are an essential part of the program, because communicating with human readers is an essential part of the programmer's job.

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, the instructions in the supplemental problems for this course state that each program listing you contain comments giving your name, your mailbox number, and an identification of assignment being solved. For example:

;;; Henry M. Walker
;;; Box Y-06
;;; Supplemental Problem 2

Although one semicolon is enough to begin a comment, most Scheme programmers use several semicolons 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.

  1. Add a comment to your Scheme program file from the previous lab, including the above information. Save the file and use the load procedure to load it into the Scheme interactive interpreter again.

Simple Equivalence:

Summary: The following predicates commonly arise in Scheme:

Predicate Example that returns True (#t) Comment
number? (number? 3.1415) Is argument a number?
symbol? (symbol? 'pi) Is argument a symbol?
boolean? (boolean? #t) Is argument a boolean value?
procedure?(procedure? car) Is argument a procedure?
eq? (eq? 'a 'a) Do arguments represent identical symbols?
eqv? (eqv? 'a 'a) Similar to eq?
equal? (equal? (+ 1 1) (sqrt 4)) Are arguments the same symbols, numbers, booleans, or lists?

To gain experience with these predicates, try the following steps:

  1. Check each of the above examples to confirm that the predicates return true (#t) as expected. In each case, explain why this result is obtained.

  2. For each of the above predicates, find two rather different examples where the result is false (#f). Again, explain why this result is obtained.

  3. eq? and eqv? produce similar results, although the internal details of how comparisons are performed within Scheme may be somewhat different. Both of these standard functions (and all other elements of Scheme) are defined formally in the Revised (5) Report on the Algorithmic Language Scheme.

    This document is available through Netscape:

    To find information on eq?, scroll down the table of contents to the main heading Standard procedures, and click on the subheader Booleans. When the Standard procedures ... Booleans page appears, scroll down to find descriptions for both eq? and eqv?. While the discussion may contain more detail that you really want or need, note that this document gives a precise statement about how Scheme works.

    Within Netscape, click the Back button once to return to the Table of Contents for the Scheme report. Since this document can serve as a valuable reference, you may want to record the URL address for future use. The easiest way to do this is to move the mouse to the Bookmarks menu and select the Add Bookmark option.

    Now, move to the Bookmarks menu again and note that this Revised (5) Report ... is listed. In the future, you can return to this page just by selecting this bookmark.

This document is available on the World Wide Web as

created June 6, 1996 by John David Stone
last revised January 7, 2000 by Henry M. Walker