Booleans and 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 #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^4 report on the algorithmic language Scheme.

    This document is available on the World Wide Web:

    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 Navigator, 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 bring up the Bookmarks menu and select the Add Bookmark operation.

    Now, move to the Bookmarks menu again and note that the Revised^4 report ... is listed. In the future, you can return to this page just by selecting this bookmark.

  4. Find two arguments so that comparisons with eq? [or eqv?] return #f, but a comparison with equal? returns #t.

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

  6. 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/courses/Scheme/lab-predicates.html

created January 22, 1997
last revised May 27, 1997
Henry Walker (walker@math.grin.edu)