CS 119 Lab 4

Objectives

  • Become familiar with procedures that return procedures
  • Practice with the let statement in Scheme.
  • Perform the following tasks in the order given.

    1. Evaluate the files words.scm and lab4.scm.

    2. Take a look at the function flip in lab4.scm. To demonstrate that this procedure returns another procedure as its value try:

            (flip se)

    The procedure that is returned takes two arguments a and b. To execute this procedure try:

        ((flip se) ‘goodbye ‘hello)
        ((flip -) 5 8)

    3. Many functions are applicable only to arguments in a certain domain and result in error messages if given arguments outside that domain. For example, sqrt requires that its argument be a number. Once a program gets an error message, it is impossible for that program to continue the computation.

    Assignment:
    Write a procedure type-check that takes as arguments a one-argument procedure f and a one-argument predicate pred. type-check should return a one-argument procedure that first applies pred to its argument; if that result is true, the procedure should return the value computed by applying f to the argument; if pred returns false, the new procedure should also return false:

    Þ (define safe-sqrt (type-check sqrt number?))
    Þ (safe-sqrt 16)
    4
    Þ (safe-sqrt ‘sixteen)
    #f

    4. Take a look at the procedure gertrude. It does some redundant computation. Try:

            (gertrude ‘rose)
            (gertrude ‘iguana)

    Assignment:
    Rewrite gertrude with a let to avoid the redundant work.

    5. Take a look at the procedure sum-square. What does this procedure do? How does it work?

    6. Email your files containing the assignments to jzimmerm@goucher.edu.