[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Read the reading on conditionals. You may also want to rescan the reading on numbers and reading on Boolean values, which includes information on predicates.
Define and test a Scheme predicate atom-or-list?
that
takes one argument and returns #t
if the argument is either an
atom (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.
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.
(if #t 'aardvark 'zebra)
(if #f 'aardvark 'zebra)
(if (null? null) 'aardvark 'zebra)
(if (null? (list 'a 'b 'c)) 'aardvark 'zebra
(if () 'aardvark 'zebra)
(if (list 'a 'b 'c) 'aardvark 'zebra)
(if 2 'aardvark 'zebra)
(if 'true 'aardvark 'zebra)
(if 'false 'aardvark 'zebra)
Define and test a Scheme procedure list-type
that takes one
argument, a list, and returns the symbol empty
if the argument
is the empty list, the symbol non-empty
otherwise.
Define and test a Scheme procedure report-victory
that takes
one argument, a real number, and returns the string "I won!"
if that number is positive, the string "You won!"
if it is
negative, and the string "It's a tie!"
if it is zero.
a. Use if
for all your tests.
b. Use cond
for all your tests.
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
Wednesday, 6 September 2000
Friday, 8 September 2000
[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/conditionals.html
Source text last modified Wed Sep 13 10:50:27 2000.
This page generated on Wed Sep 13 10:50:29 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu