CSC 151-02, Fall 2006 : Schedule : Lab 8


Lab 8: Boolean Values and Predicates

In this lab, you will have the opportunity to explore a number of issues relating to predicates, Boolean values, and conditional operations.

Procedures covered in this lab include:


Exercises

Exercise 0: Preparation

Although this laboratory focuses on Boolean operations, it also uses some numeric predicates. You may therefore want to rescan the reading on numbers.

After making sure that you're prepared, start DrScheme.

Exercise 1: Type Predicates

Fill in a few interesting entries in following table. You need not fill in the whole table; simply do as much as you think gives you a good sense of the various predicates.

5 5.0 'five "five" list #t #f (cons 'a null) null 'null ()
number?                      
symbol?                      
string?                      
procedure?                      
boolean?                      
list?                      

Exercise 2: Empty lists

Which of the following does Scheme consider an empty list?

Exercise 3: Equality

Consider the following definitions

(define alpha (list 'a 'b 'c))
(define beta (list 'a 'b 'c))
(define gamma alpha)
(define delta (cons (car alpha) (cdr alpha)))

Determine which of the lists are eq?, eqv?, or equal?.

Exercise 4: Reflection

What, if anything, did you find surprising in the results of the previous exercises?

By looking at the Scheme report, see if you can find a pair of values that are equal in the sense of = but not in the sense of eqv.

Exercise 5: What is not?

a. What type is not?

b. What predicate would you use to verify your answer?

Exercise 6: What is not? Revisited

The symbol not is the name of something, as you determined in the preceding exercise. However, the symbol itself, considered as a datum, is not a procedure. Does Scheme agree with this classification? How could one ask Scheme whether the symbol not is a procedure?

Exercise 7: Combining Boolean Values

Fill in the following tables for each of the operations and and or.

and

First argument Second argument Result
#f #f  
#f #t  
#t #f  
#t #t  

or

First argument Second argument Result
#f #f  
#f #t  
#t #f  
#t #t  

Exercise 8: Ranges

a. Write a Boolean expression that determines if the value named by grade is between 0 and 100, inclusive.

b. Test that expression using different values of grade.

Exercise 9: Exploring and and or

a. Determine the value and returns when called with no parameters.

b. Explain why you think the designers of Scheme had and return that value.

c. Determine the value and returns when called with integers as parameters.

d. Explain why you think the designers of Scheme had and return that value.

e. Determine the value or returns when called with no parameters.

f. Explain why you think the designers of Scheme had or return that value.

g. Determine the value or returns when called with only integers as parameters.

h. Explain why you think the designers of Scheme had or return that value.

Exercise 10: What is it?

Define and test a Scheme predicate symbol-or-list? that takes one argument and returns #t if the argument is either a symbol or a list, #f if it is neither.

Exercise 11: Between, Revisited

Define and test a Scheme predicate between? that takes three arguments, all real numbers, and determines whether the second one lies strictly between the first and third (returning #t if it is, #f if it is not). For example, 6 lies strictly between 5 and 13, so both (between? 5 6 13) and (between? 13 6 5) should have the value #t.

Exercise 12: Triangulation

Three line segments can be assembled into a triangle if, and only if, the length of each of them is less than the sum of the lengths of the other two. Define a Scheme predicate triangle? that takes three arguments, all positive real numbers, and determines whether line segments of those three lengths (assumed to be measured in the same units) could be assembled into a triangle.

Note that you will have to use a combination of ands and ors to solve this problem.

Exercise 13: Safer Computation

You may recall the following alternate addition procedure from the reading.

;;; Procedure:
;;; safe-add
;;; Parameters:
;;; x, a number [verified]
;;; y, a number [verified]
;;; Purpose:
;;; Add x and y.
;;; Produces:
;;; sum, a number.
;;; Preconditions:
;;; (No additional preconditions)
;;; Postconditions:
;;; sum = x + y
;;; Problems:
;;; If either x or y is not a number, sum is #f.
(define safe-add
(lambda (x y)
(and (number? x) (number? y) (+ x y))))

Define similar procedures, safe-subtract and safe-multiply, that confirm that their parameters are numbers before subtracting and multiplying, respectively.

What is the advantage of defining such procedures?

Exercise 14: Safer Division

You may note that the divide procedure, /, produces an error if the divisor is 0. We might, therefore, hope for a safer version that returns #f rather than giving up, when the divisor is 0 (or when either dividend or divisor is not a number).

Define a procedure, safe-divide, that does just that.

Exercise 15: Identifying Oddness

a. Consider the expression (and (integer? x) (odd? x) x). What value does it return if x is not an integer? If x is even? If x is odd?

b. Using what you've determined about the previous expression, write a procedure, (first-odd i1 i2 i3), that takes as parameters three integers and returns the first odd value. If none of the integers is odd, first-odd should return #f.

Notes

Notes on Problem 9

(and) has value true (#t) because and has a value of true if none of the parameters have value false. Since this calls has no parameters, none are false.

(or) has value false (#f) because or has value false if none of the parameters is non-false. Since this call has no parameters, none are non-false.


Janet Davis (davisjan@cs.grinnell.edu)

Created September 7, 2006 based on http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/boolean.html
Last revised September 7, 2006
With thanks to Sam Rebelsky