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

User-Defined Procedures

Summary: This lab provides practice with user-defined procedures (lambda expressions), the Emacs editor for writing Scheme programs, and script for recording sessions.


User-Defined Procedures

  1. Consider the following sequence of definitions computes the volume of a sphere of radius 5:

    
    (define pi 3.14159265358979)
    (define r 5)
    (define r-squared (* r r))
    (define r-cubed (* r r-squared))
    (define pi-r-cubed (* pi r-cubed))
    (define volume (* 4/3 pi-r-cubed))
    

    Highlight this code in your browser by moving the mouse to its start, and holding down the left mouse button while moving the mouse to its end. After the section is highlighted, release the left mouse button.

    Now start Scheme in a terminal window, and paste this highlighted material into Scheme by moving the mouse to the terminal window and clicking the middle mouse button.

    Describe the results of this work.

    Retrieve the value computed by typing volume into Scheme.

  2. Check (by hand or with a calculator) that this sequence produces the correct result.

  3. In the last expression, why are no parentheses used around the number 4/3?

    Does the computation still come out correctly if parentheses are used around 4/3? Why or why not?

  4. Multiplication * as an n-ary operation: In the above definition of volume, the multiplication operator * is used as a binary operator -- applying only to 2 operands. Scheme, however, defines the multiplication operator * as applying to as many operands as desired. For example, the computation of step 1 be written in one define statements as follows:
    (define volume (* 4/3 3.14159265358979 5 5 5))
    
    Check the values Scheme returns for each of the following expressions:
       (* 2)
       (* 2 2)
       (* 2 2 2)
       (* 2 2 2 2)
       (* 2 2 2 2 2)
    
    What happens if you do not supply any operands?
       (*)
    
    Hypothesize why you get this result.

    Follow a similar pattern using addition + rather than multiplication.

  5. The Quote Procedure: Use the quote procedure to print the value of the symbol pi, rather than its value. That is, enter the following into Scheme
        (quote pi)
    
    (quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.


Using Emacs

  1. Creating New Scheme Files: Clicking with the left mouse button on the icon of gnu (African antelope) at the bottom of the screen to open an Emacs window.

  2. Type the following Scheme definitions into the Emacs file.

    
         (define pi 3.14159265358979)
         (define q 'quarts)
         (define a (sqrt 2))
    

    As you are typing a right parenthesis, Emacs has the capability to show you which left parenthesis it matches. If Emacs does not do this for you automatically, bring your mouse to the Options menu at the top of the Emacs window, and select the option for Paren Match Highlighting. This will be particularly helpful when typing longer Scheme programs.

    Save the file by clicking on the save button at the top of the Emacs window. Emacs 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.

  3. To run a program, you will want to open a terminal 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 terminal window to the lower-left of the screen, so you can see much of both the Emacs and terminal windows at the same time. (Firefox still will be at the upper-left of the screen.)

    Move the mouse to the terminal 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
    
  4. Editing Existing Scheme Files: For practice, close the Emacs window using the Exit Emacs option under the File menu. To open and re-edit first-test.ss again click on the gnu icon to start Emacs. 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.)

  5. 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 terminal window or the Emacs 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?

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

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


Lambda Expressions

  1. Apply this function to the values 1, 3, -2 by typing:
        ((lambda (x) (* x x)) 1)
        ((lambda (x) (* x x)) 3)
        ((lambda (x) (* x x))-2)
    
    What happens if you try to apply the procedure to all three values at once?
        ((lambda (x) (* x x)) 1 3 -2)
    
    In Scheme, a lambda expression is called a procedure.

  2. Naming Procedures: Use define to bind a lambda expression to a symbol; we call the above function f, so the above computations could be done as follows:
        (define f
            (lambda (x) (* x x)))
        (f 1)
        (f 3)
        (f -2)
    

  3. Define a procedure f(x) = x² - 3x + 2 and evaluate it for x = 0, 1, 2, 3, 4 .

  4. Define a procedure that computes the volume of a sphere, given its radius.


Recording a Session:

  1. Record your work in a file for parts 7, 13, 14, 15, and 16 of this lab by using the following steps.

Work to be turned in:

This document is available on the World Wide Web as

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

created 22 January 1977
last revised 23 January 2006
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.