Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Summary: 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:
boolean?
,
integer?
,
list?
,
null?
,
number?
,
pair?
,
procedure?
,
symbol?
=
,
eq?
,
eqv?
,
equal?
,
<
(strictly less than),
<=
(less than or equal to),
=
(equal to),
>=
(greater than or equal to),
>
(strictly greater than)
and
,
or
,
not
not
?
and
and or
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.
Fill in a few interesting
entries in following table.
You need not fill int 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? 
Note: You can fill in a column very quickly if you define a procedure that, given a value, makes a list of the results of applying each predicate to that value. Here's a start.
(define col (lambda (val) (list 'number? (number? val) 'symbol? (symbol? val))))
Which of the following does Scheme consider an empty list?
null
'null
()
(list 'a)
(list)
'nothing
(cdr (list 'a))
not
?
a. What type is not
? That is, is it a number, a symbol,
a string, ...?
b. What predicate would you use to verify your answer? That is, what value would you use for the blank in the following?
> (_____ not)
#t
c. What type is 'not
? That is, is it a number, a symobl,
a string, ....?
d. What predicate would you use to verify your answer? That is, what value would you use for the blank in the following?
> (_____ 'not)
#t
Fill in the following tables for each of the operations and
and or
. In the first table, the result column should be
the value of (and arg1 arg2)
, where arg1
is
the first argument and arg2
is the second argument. In
the second table, the result column should be the value of
(or arg1 arg2)
.
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 
Define a predicate, (bigger? num1 num2)
, that takes two
real numbers as arguments and returns #t
if the absolute
value of the first is greater than the absolute value of the second,
#f if it is not.
Call your procedure twice  once with arguments that ensure that the value of the procedure call is #t, once with arguments that make the value #f.
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
.
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.
If you are puzzled by some of the answers, you may want to look at the notes on this problem, available at the end of the lab.
Define and test a Scheme predicate, (primitive? val)
,
that returns #t
if val is a symbol, number, character,
or Booelan value, and #f
otherwise.
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
.
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? side1 side2 side3)
, 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.
You may recall the following alternate addition procedure from the reading.
;;; Procedure: ;;; safeadd ;;; 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 safeadd (lambda (x y) (and (number? x) (number? y) (+ x y))))
Define similar procedures, safesubtract
and
safemultiply
, that confirm that their parameters are
numbers before subtracting and multiplying, respectively.
What is the advantage of defining such procedures?
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, safedivide
, that does just that.
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, (firstodd i1 i2 i3)
, that
takes as parameters three integers and returns the first odd value.
If none of the integers is odd, firstodd
should return
#f
.
(and)
has value true (#t
) because
. Since this calls has no parameters, none are false.
and
has a value of true if none of the parameters have
value false
Alternately, you can think of #t
as the anditive identity
.
That is, (and #t x)
is x
.
(or)
has value false (#f
) because
. Since this call has no parameters, none are nonfalse.
or
has value false if none of the parameters is
nonfalse
Alternately, you can think of #f
as the oritive identity
.
That is, (or #f x)
is x
.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151//History/Labs/boolean.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:16 2007.
The source to the document was last modified on Mon Feb 5 10:36:55 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Labs/boolean.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.