Editing Scheme Files

Editing Scheme Files

Editing Scheme Files

Goals: This laboratory exercise introduces the use of the XEmacs editor for writing Scheme programs, provides more experience with symbols and expressions in Scheme, and outlines a mechanism for recording sessions at a workstation.

In the previous lab, you typed commands directly into the Scheme environment, which immediately evaluated and printed the result. While this use of Scheme is quite straightforward, it has at least two disadvantages. First, all program definitions must be retyped every time a program is to be run, as nothing is saved from one use of Scheme to the next. Second, the results of a computation are not saved, and thus they are difficult to print. This lab addresses both of these issues.

  1. Creating New Scheme Files: We will use the XEmacs editor to create a file for a Scheme program. This is done by clicking on the pencil-and-paper icon on the bottom row of the front panel in HP VUE. Presently, a new window will appear for your use in entering your program.

    Next, give XEmacs the name of the file that you want to create or revise. Click on the dogeared-paper icon at the left end of the toolbar near the top of the XEmacs window -- the one with the word Open underneath it. A dialogue box pops up, with the names of various editable files displayed above and a prompt at the bottom that looks like this:

    Find file: ~/
    
    Move the mouse pointer into the dialogue box and type in the name of the file you want to create. Conventionally, Scheme programs are stored in files with names ending in .ss. For example, you might call the first Scheme file that you create first-test.ss. Type this name in at the Find file prompt, after the slash, and hit the Enter key. (Do NOT click on the OK button.)

    Often, it is convenient to move the XEmacs window to the right of the computer screen. This is accomplished by moving the mouse to the labelled bar emacs:... at the top of the XEmacs window. Press the left mouse button on this labelled bar, and move the mouse (keeping the left button depressed). The XEmacs window will follow your mouse movements. When the XEmacs window is where you want it, release the left mouse button. Similarly, you may want to move the dtterm window to the left of the screen, so you can see much of both windows at the same time.

    While XEmacs is an extremely powerful editor, many common capabilities are highlighted with buttons and menus at the top of window. These menus are analogous to most word processing packages, and thus are not discussed here. Ask the instructor as questions arise. (If something particularly strange seems to be happening, type <Ctrl/g> to stop the processing of a command.)

    Type the following Scheme definitions into the XEmacs file.

    
         (define pi 3.141596535)
         (define q 'quarts)
         (define a (sqrt 2))
    
    As you are typing, note that when you type a right parenthesis, XEmacs shows you which left parenthesis it matches. This will be particularly helpful when typing longer Scheme programs.

    Save the file by clicking on the save button at the top of the XEmacs window -- the one that shows a diskette icon. File first-test.ss now is ready for use within Scheme.

    Return the mouse to the dtterm window and type scheme to begin running the Scheme environment. Within Scheme, you can use the definitions from a file with the load procedure. Here, you should type

    
        (load "first-test.ss")
    
    More generally, load allows you to specify any file by placing the file name in double quotes.

    Check that the definitions from the file work as expected by typing

    
        pi
        q
        a
    
  2. Symbols and Expressions: Type some additional definitions into first-test.ss, save the changes, and reload the file into scheme with the same load command. Note that you do not need to close either the dtterm or the XEmacs window as you work -- you can just move the mouse from window to window as needed.

    Modify first-test.ss so that it contains a typographical error. (Remember to save the file again.) What happens when you try to load this version of the file into scheme?

  3. What happens if you place an arithmetic expression into first-test.ss? For example, include the line
    
        (+ 2 3)
    
    in the file and load it into scheme. Describe what happens.

  4. Add the following definitions to first-test.ss:
    
        (define r (+ 2 3))
        (define s '(+ 2 3))
        (define t (quote (+ 2 3)))
        (define u ''(+ 2 3))
        (define v (quote (quote (+ 2 3))))
        (define w (+ 1/2 1/3))
    
    Load the revised file into Scheme and check the definitions for each of the variables r, s, t, u, v, w. Explain the results that you observe.

  5. Recording a Session: Your activity at a workstation may be recorded in a file using the following steps.
    • Within the dtterm window (before running scheme), begin a recording session with the statement
      
          submit first.session
      
      
      This indicates that what follows will be recorded in a file called first.session.
      Be sure the file name used here is different from the file name used previously for your Scheme definitions. [Why?]
    • Print a copy of your Scheme definitions with the command
      
          cat first-test.ss
      
    • Run scheme as usual, with the scheme command.
    • Load file first-test.ss with the load command, and check the definitions as before.
    • Stop scheme with <Ctrl/D> as before.
    • Stop recording by typing <Ctrl/D> one more time.
    • Print the record of your session by typing
      
      print first.session
      
    • Your printout will appear at the printers in the lab (Science 2417). If several printouts seem rather similar, you may be able to identify yours by looking for the name of the workstation from which you sent the print job. Thus, if you are working at workstation viete, then viete will appear on any title page and as the workstation prompt on your recorded session.

  6. Textbook exercises: If you have not already finished exercises 1.2-1.5 in the textbook, be sure you have them done before the next lab.

  7. Additional Practice: As you have time, experiment further with define and quote. For example, try to anticipate what happens with the following:
    
    (define x 2)
    (define y 3)
    (+ x y)
    (+ 'x 'y)
    ('+ x y)
    '(+ x y)
    
    Then try entering these lines into Scheme, and observe the results. Be sure you can explain what happens.
Work to be turned in:

This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/151.fa98/lab-editing.html

created January 13, 1997
last revised August 30, 1998