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

Simple Equivalence in Scheme

Goals

This laboratory exercise has both a pragmatic and a conceptual segment.


Scheme Within Emacs

Emacs may be customized to simplify Scheme programming in two ways:

  1. Customize Emacs for Chez Scheme programming: Startup instructions for Emacs are stored in a configuration file, called .emacs, that is located in your home directory. To customize Emacs for Scheme, follow these steps:
    1. Start Emacs 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/walker/153/emacs-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 Emacs window.

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

  2. Check whether the customization succeeded: Restart Emacs and open a new file named sample.ss in the Emacs window. Look at the "mode line" at the bottom of the Emacs window; it tells you what file you're currently editing and which of Emacs's numerous editing modes are active. If you see the word Scheme somewhere on that line, you did step 1 correctly.

  3. Experiment with formatting of Scheme within Emacs:

    1. Type in the following Scheme definition:

      (define h5
      (+ 1/1 1/2 1/3 1/4 1/5))
      
    2. Place the editing cursor anywhere on the second line of the definition and press the <Tab> key. Notice that Emacs indents the line. The number of spaces that Emacs inserts is determined by a set of conventions that have been found to result in highly readable programs.

  4. Run Scheme within Emacs:

    The customization that you performed in step 1 enables you to run Chez Scheme inside the Emacs window rather than in the terminal window, if you wish. The choice between using a terminal window or Emacs 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. To run Chez Scheme inside Emacs, press the <F1> key. Your Emacs window will split into two parts, and the Chez Scheme interactive interface will be started in one subwindow.

    2. Move your cursor to the top (Scheme) window, and enter a Scheme expression, such as (+ 2 3). Press the <Enter> key to check that Scheme operates directly on what you have typed.

    3. Move your cursor to the bottom (editing) window, placing it at the end of the definition that you typed in back in step 3a and press <Alt/Enter>; that is, hold down the <Alt> key and simultaneously press <Enter>. 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.

    4. Move the editing cursor to the position immediately following this second prompt in the top window, move the cursor to the beginning of the next line, type h5, 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 4c.

  5. When Chez Scheme is running in an Emacs window, you can't shut it down with <Control/D>, since Emacs 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.

  6. Press <F1> again, so that Scheme is running in one window and you are editing a file in another.

    Try exiting from Chez Scheme before closing the Emacs window in which it is running. Emacs will remind you by printing the question

    Active processes exist; kill them and exit anyway? (yes or no)
    

    at the bottom of the window. Type the word yes in response; Emacs will shut down Chez Scheme for you as it shuts itself down.


Comments in Scheme

A Scheme comment begins with a semicolon and extends to the end of a line. 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:

The reading for this lab included the following table of common Scheme predicates:

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?

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.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/153.sp05/labs/lab-simple-predicates.shtml

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