Laboratory Exercises For Computer Science 151

User-Defined Procedures

User-Defined Procedures

Goals: This lab provides practice with simple, user-defined procedures, and it introduces the use of the XEmacs editor for writing Scheme programs.

User-Defined Procedures

  1. Write a procedure (quadratic-root a b c) that finds one root of a quadratic equation
    ax² + bx + c = 0 using the quadratic formula. Use it to find a root of the above equation.

    Test your procedure by computing

     
         (quadratic-root 1 -5 6)
         (quadratic-root 2 -10 12)
         (quadratic-root 1 4 4).
    
    In each case, use algebra to check your answers.

    What are (quadratic-root 1 0 1) and (quadratic-root 1 0 2)?
    [Would these two examples work in other programming languages that you know?]

  2. Textbook Exercise: Work on exercise 1-22 from the textbook.

  3. The following code shows two proposed solutions the problem stated in exercise 1-27:
    (define semip
        (lambda (x y z)
            (/ (+ x y z) 2)))
    (define solution1
        (lambda (x y z)
            (sqrt (* (semip x y z) 
                     (- (semip x y z) x)
                     (- (semip x y z) y)
                     (- (semip x y z) z)))))
    
    (define helper
        (lambda (s x y z)
            (sqrt (* s (- s x) (- s y) (- s z)))))
    (define solution2
         (lambda (x y z)
            (helper (/ (+ x y z) 2) x y z)))
    


Using XEmacs

So far, you have 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 part of the 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 with the left mouse button on the icon of a paper and pencil at the bottom of the screen. Shortly, a new window will appear for your use in entering your program.

    Often, it is convenient to move this window to the right the computer screen. This is accomplished by moving the mouse to the labeled bar emacs:... at the top of the XEmacs window. Press the left mouse button on this labeled 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.

    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. XEmacs then will open a new window, asking you to give a file name for the program. For example, to save your work in a file first-test.ss, you could type this name into the new file-naming window and hit the return key. File first-test.ss now is ready for use within Scheme.

  2. To run a program, you will want to open an dtterm window. As in the previous labs, this may be done by clicking on the icon of a computer screen and keyboard. This time, you may want to move the dtterm window to the lower-left of the screen, so you can see much of both the XEmacs and dtterm windows at the same time. (Netscape still will be at the upper-left of the screen.)

    Move 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
    
  3. Editing Existing Scheme Files: For practice, close the XEmacs window using the Exit XEmacs option under the File menu. To open and re-edit first-test.ss again click on the paper and pencil icon to start XEmacs. Then click on the Open button. A new window will appear, showing the names of some files. You may select first-test.ss by clicking on this name and pressing return. (Alternatively, you may type the name first-test.ss into the open file window.)

  4. 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 by clicking on the save button.) What happens when you try to load this version of the file into scheme?

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

  6. 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))))
    
    Load the revised file into Scheme and check the definitions for r, s, t, u, and v. Explain the results that you observe.

  7. Recording a Session: Your activity at a workstation may be recorded in a file using the following steps.
Work to be turned in:

This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/153/lab-procedures.html

created January 22, 1997
last revised January 12, 1998