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 Booleans. When the Standard procedures ... Booleans 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.

    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 (4) Report ... is listed. In the future, you can return to this page just by selecting this bookmark.

  4. Review pp. 26-29 in the textbook, and 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.

  5. Run the example that starts on the bottom of page 26 in the textbook, to be sure you understand the results obtained.

  6. Textbook Exercises: Do exercises 1.10 and 1.11 from the textbook. In each case, the exercise asks you to state a conclusion. After you have a tentative answer, run several examples to test whether your conclusion is true, and revise your answer if necessary. Once you are confident in your answer, argue why your conclusion is correct.

  7. Textbook Exercises: Work on exercises 1.12-1.15 from the textbook. In each case, check your answer by entering the expression into Chez Scheme. Be sure you can justify each result.

This document is available on the World Wide Web as

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

created January 22, 1997
last revised September 4, 1998