CSC 153 Grinnell College Spring, 2009
 
Computer Science Fundamentals
 

User-Defined Procedures

Summary: This lab introduces user-defined procedures (lambda expressions), the specification of comments within Scheme programs, and some Scheme predicates — procedures that return Boolean values.


Files with DrScheme

  1. Start DrScheme by clicking on the red, white, and blue lambda symbol on the front panel.

  2. Enter the following into the top definitions window:

    
        (+ 4 5)
        (* 4 5)
    

    Click the "Run" button, and observe what is printed in the interactions window.

  3. Add the following definitions to the top definitions.

    
        (define r (+ 2 3))
        (define s '(+ 2 3))
        (define t (quote (+ 2 3)))
        (define u ''(+ 2 3))
        (define v (quote (quote (+ 2 3))))
    

    Before your click "Run", move to the interactions window. What happens when you type r, s, t, u, v?

    Now click "Run", and describe what happens.

    1. What is printed (is it different than step 2)?
    2. Now what happens when you type r, s, t, u, v in the interactions window?
  4. Use the "File" menu to save the material in the definitions window; a common convention is use .ss as the ending for Scheme programs. Following this convention, name this first program file lab2.ss

  5. Shut down DrScheme, and then start it up again. Now load lab2.ss into DrScheme, and execute the program.


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 paste this highlighted material into the definitions window of DrScheme by moving the mouse to the definition window and clicking the middle mouse button.

    Describe the results of this work.

    Now click "Run" and 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.
  6. Review the results you obtained in part 3 for symbols r, s, t, u, v. In each case, explain why the result was obtained.


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.


Work to be turned in:


This document is available on the World Wide Web as

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

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