CSC 153: Computer Science Fundamentals Grinnell College Spring, 2005
Laboratory Exercise Reading

Simple Equivalence in Scheme


This reading involves both a pragmatic and a conceptual segment. The practical part describes some ways to use Emacs 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 Emacs

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

Within Emacs, 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 Emacs 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 Emacs window -- thereby avoiding the need to reload code into a terminal window.

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

Customizing Emacs for Chez Scheme programming

Files containing Chez Scheme source code conventionally end in .ss ("Scheme source"). The Scheme mode supplied with Emacs does not recognize this convention (it assumes .scm instead), but a Chez Scheme programmer can make Emacs aware of it by revising the Emacs configuration file, .emacs, in her home directory. The revision need only be made once. Afterwards, Emacs will automatically be reminded of the .ss convention every time it is started up.

The Emacs configuration file is actually a short computer program that is executed while Emacs 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 Emacs to work with Chez Scheme programs.)

The customizations that you need to add are stored in the file /home/walker/153/emacs-customization. During the lab itself, the instructions will guide you through inserting this customization file into your Emacs configuration file, .emacs.

Running Chez Scheme in an Emacs window

The above customization also enables you to run Chez Scheme inside the Emacs window, if you wish. Inserting the customization with .emacs provides two convenient additional capabilities:

  1. When you press the <F1> key, your Emacs window will split into two parts, and the Chez Scheme interactive interface will be started in one subwindow. This allows you to run Schem in one window and edit a file in a section −− all within Emacs.

  2. With both windows visible within emacs, you can enter Scheme expressions directly from the editing window into Scheme. More specifically, once you have typed a Scheme expression in the editing window, you can enter it directly into Scheme by

    1. placing the cursor at the end of the expression, and
    2. pressing <Alt/Enter> (holding down the <Alt>key and pressing <Enter>).

Of course, it is also possible to cut and paste between subwindows within Emacs, 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.

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 Science Office
;;; 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.

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? sqrt) 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?

Each of these predicates return ture (#t) if the specified condition is satisfied, and false (#f) if the condition is not satisfied.

This document is available on the World Wide Web as

created 6 June 1996 by John David Stone
last revised 24 January 2006 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at