Fundamentals of Computer Science I (CS151 2003F)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
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
?
not
? Revisited
and
and or
You may find it helpful to rescan the readings on Boolean values and conditionals.
You may also 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? 
Which of the following does Scheme consider an empty list?
null
'null
()
(list a)
(list)
'nothing
Consider the following definitions
(define alpha (list 'a 'b 'c)) (define beta (list 'a 'b 'c)) (define gamma alpha)
Determine which of the lists are
eq?
,
eqv?
,
or
equal?
.
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
.
not
?
a. What type is not
?
b. What predicate would you use to verify your answer?
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?
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 
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.
Define and test a Scheme predicate symbolorlist?
that
takes one argument and returns #t
if the argument is either a
symbol or a list, #f
if it is neither.
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?
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 should try to define triangle?
without if
(although it is certainly acceptable to use if
in the
definition).
Define and test a Scheme procedure neighbor
that takes one
argument, an integer, and returns the next higher integer if its argument
is even, the next lower integer if its argument is odd. (Start by writing
a comment that describes the purpose of the procedure.)
For each of the following expressions, guess what the output should be and then test it in Scheme.
a. (if #t 'aardvark 'zebra)
b. (if #f 'aardvark 'zebra)
c. (if (null? null) 'aardvark 'zebra)
d. (if (null? 'null) 'aardvark 'zebra)
e. (if (null? (list 'a 'b 'c)) 'aardvark 'zebra)
f. (if () 'aardvark 'zebra)
g. (if (list 'a 'b 'c) 'aardvark 'zebra)
h. (if 2 'aardvark 'zebra)
i. (if 'true 'aardvark 'zebra)
j. (if 'false 'aardvark 'zebra)
Define and test a Scheme procedure listtype
that takes one
argument, a list, and returns the symbol empty
if the argument
is the empty list and the symbol nonempty
otherwise.
Define and test a Scheme procedure reportvictory
that takes
one argument, a real number, and returns the symbol won
if that number is positive, the symbol lost
if it is
negative, and the string tied
if it is zero.
a. Use if
for all the tests within your procedure.
b. Use cond
for all the tests within your procedure.
As you may know, one of the famous riddles of the Sphinx goes something like the following:
What is it that walks upon four legs, then two legs, then three legs?
The answer is humans
.
Write a Scheme predicate, legs
, that, given someone's age, tells
how many legs they walk upon. (You get to choose reasonable ages
for the three phases of life.)
Write and test a Scheme procedure, type
, that returns the a symbol
that represents the type of
its argument. For example,
> (type 3) integer > (type '(1 2 3)) list > (type "hello") string > (type 2/3) rational > (type 'type) symbol > (type type) procedure > (type 1.2) real > (type 3+4i) complex
You need only cover the types in the example above, but you should cover all of those types.
Warning! As you've noted in the past, you should not be able to distinguish between real numbers and rational numbers. I would recommend that you use rational for exact reals/rationals and real for inexact reals/rationals.
(and)
has value true because
. Since it
has no parameters, none are false.
and
has a
value of true if none of the parameters have value false
(or)
has value false because
. Since it has no parameters,
none are nonfalse.
or
has value false
if none of the parameters is nonfalse
Wednesday, 6 September 2000 [Samuel A. Rebelsky]
Friday, 8 September 2000 [Samuel A. Rebelsky]
Wednesday, 31 January 2001 [Samuel A. Rebelsky]
and
and or
with no parameters.
Thursday, 1 February 2001 [Samuel A. Rebelsky]
and
and or
with no parameters.
Wednesday, 7 February 2001 [Samuel A. Rebelsky]
Tuesday, 10 September 2002 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151//2002F/Labs/conditionals.html
.
Tuesday, 28 January 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153//2003S/Labs/conditionals.html
.
Tuesday, 4 February 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151//2003S/Labs/conditionals.html
.
Monday, 15 September 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151//2003F/Labs/conditionals.html
.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[Honesty]
[Instructions]
[Links]
[Guidelines for Lab Writeups]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[Scheme Report]
[Glimmer Scheme Reference]
[CSC151.01 (Gum)]
[CSC151 2003S]
[CSC151 2002F]
[SamR]
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 Tue Dec 9 13:58:58 2003.
The source to the document was last modified on Mon Sep 15 10:29:21 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Labs/conditionals.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu