Laboratory Exercises For Computer Science 151

An Introduction to Scheme Predicates

An Introduction to Scheme Predicates

Goals: This lab provides practice with Scheme predicates, including predicates that test the type of an expression or the equivalence of expressions.

Summary: The textbook discusses the following common Scheme predicates:

Predicate Example that returns True (#t) Comment
number? (number? 3.1415) Is argument a number?
symbol? (symbol? 'pi) Is argument a symbol?
boolean? (boolean? #t) Is argument a boolean value?
pair? (pair? '(a b)) Is argument a [cons] pair?
null? (null? '()) Is argument the empty list?
procedure?(procedure? car) Is argument a procedure?
eq? (eq? 'a 'a) Do arguments represent identical symbols?
eqv? (eqv? (car '(a a)) 'a) Similar to eq?
equal? (equal? '(b c) (cdr '(a b c))) Are arguments the same symbols, numbers, booleans, or lists?

Steps for this Lab:

  1. Check each of the above examples to confirm that the predicates return true (#t) as expected. In each case, explain why this result is obtained.

  2. For each of the above predicates, find two rather different examples where the result is false (#f). Again, explain why this result is obtained.

  3. eq? and eqv? produce similar results, although the internal details of how comparisons are performed within Scheme may be somewhat different. Both of these standard functions (and all other elements of Scheme) are defined formally in the Revised (5) Report on the Algorithmic Language Scheme.

    This document is available through Netscape:

    To find information on eq?, scroll down the table of contents to the main heading Standard procedures, and click on the subheader Equivalence predicates. When the Standard procedures ... Equivalence predicates page appears, scroll down to find descriptions for both eq? and eqv?. While the discussion may contain more detail that you really want or need, note that this document gives a precise statement about how Scheme works. Also, the description contains several examples.

    Within Netscape, click the Back button once to return to the Table of Contents for the Scheme report. Since this document can serve as a valuable reference, you may want to record the URL address for future use. The easiest way to do this is to move the mouse to the Bookmarks menu and select the Add Bookmark option.

    Now, move to the Bookmarks menu again and note that this Revised (5) Report ... is listed. In the future, you can return to this page just by selecting this bookmark.

  4. Use the Revised (5) Report ... to answer each of the following:

    1. Find a procedure which gives the integer quotient of two integer values; that is, the procedure should return the quotient of two integers, ignoring any remaineder. Thus, the integer quotient of 7 and 2 should be 3.

    2. Find a procedure which returns the denominator of a fraction (after reducing to lowest terms). Thus, this procedure should return 2 when applied to both 17/2 and 34/4.
    3. Find a procedure which returns the largest number on a list of numbers.

  5. Run the following comparisons:
    
    (eq? 'a 'a)        (eqv? 'a 'a)        (equal? 'a 'a)
    (eq? 'a 'b)        (eqv? 'a 'b)        (equal? 'a 'b)
    (eq?  3  3)        (eqv?  3  3)        (equal?  3  3)
    (eq?  3  4)        (eqv?  3  4)        (equal?  3  4)
    (eq? '(a) '(a))    (eqv? '(a) '(a))    (equal? '(a) '(a))
    (eq? "abc" "abc")  (eqv? "abc" "abc")  (equal? "abc" "abc")
    
    In each case, be sure you can explain the results obtained.

  6. Run the following sequence of definions and expressions, and explain the results obtained.
    
    (define ls1 (list 'a 'b 'c))
    (define ls2 (list 'a 'b 'c))
    (define ls3 ls1)
    ls1
    ls2
    ls3
    (eq? (list 'a 'b 'c) (list 'a 'b 'c))
    (equal? (list 'a 'b 'c) (list 'a 'b 'c))
    (eq? ls1 ls2)
    (equal? ls1 ls2)
    (eq? ls1 ls3)
    (equal? ls1 ls3)
    

This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/151.fa00/lab-predicates.html

created January 22, 1997
last revised September 1, 2000