Fundamentals of Computer Science I (CS151.01 2006F)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
This lab is also available in PDF.
Summary: In this lab, you will have the opportunity
to explore Scheme's two primary conditional control operations,
if
and cond
.
Contents:
You may find it helpful to rescan the readings on Boolean values and numbers.
After making sure that you're prepared, start DrScheme.
Define and test a Scheme procedure, (reportvictory
score)
, that takes one argument, a real number, and returns
the symbol won
if score 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.
Write a procedure, (typeof val)
, that returns
boolean
, if val is a boolean;
character
, if val is a character;
number
, if val is a number;
procedure
, if val is a procedure;
string
, if val is a string;
symbol
, if val is a symbol; or
miscellaneous
, if val is anything else.
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.
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, of course, 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.)
Although the reading on conditionals
suggests using if
for the disparity
procedure, it
is certainly possible to write that procedure without conditionals by
simply using subtraction and absolute value. Do so.
You may recall a previous question that asked for the following:
Define and test a Scheme procedure (reportvictory score)
that takes
one argument, a real number, and returns the symbol won
if score is positive, the symbol lost
if it is
negative, and the string tied
if it is zero.
In that problem, you wrote one version that used if
and
one version that used cond
. However, it is also possible
to write reportvictory
using only and
,
or
, and not
. Try doing so.
Write a procedure, (validdate? month day)
,
that returns #t
if the numbers month and day
describe a valid month and day and false otherwise.
For example,
> (validdate? 1 30) #t > (validdate? 2 30) #f > (validdate? 9 30) #t > (validdate? 9 31) #f > (validdate? 9 1) #f > (validdate? 9 250) #f
a. Write this procedure using if
as the primary control structure.
b. Write this procedure using cond
as the primary control structure.
c. Write this procedure without if
and cond
(that is, using and
, or
, and, possibly, not
).
Write a procedure, (classifynumber num)
, that
creates a string that classifies num according to
its exactness and
its least general type (selected from integer
,
rational
, and complex
).
> (classifynumber 25) "exact integer" > (classifynumber 3.5) "inexact rational" > (classifynumber 3+4i) "exact complex">
You can assume that num is a number.
Write a procedure, (classifynumber num)
, that returns a string
that describes num subject to the following restrictions.
"zero"
;
exact
or inexact
:
rational; and
real.
For example,
> (classifynumber 23) "exact positive integer" > (classifynumber #i23) "inexact positive integer" > (classifynumber 2.3) "inexact negative real" > (classifynumber 24/7) "exact positive rational"
To write the procedure reportvictory
without if
and cond
, we need to take advantage of the way and
and or
behave. Recall that and
either returns false
(if it encounters a false) or the last value (if it fails to encounter a
false value) and that or
returns the first nonfalse value,
if one is there.
Hence, we'll use an and
for each test and group them together
within an or
.
The first test looks something like this:
(and (> score 0) 'won)
. Note that this test
returns #f
if score is less than or equal to 0 and
the symbol won
otherwise.
We can then put that in an or
expression. Here's a start.
(or (and (> score 0) 'won) 'somethingelse)
Now, if score is greater than 0, we get won
and
otherwise we get somethingelse
.
The remainder is up to the reader to figure out.
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
.
Saturday, 9 September 2006 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151//2006F/Labs/boolean.html
.
typeof
problem (without first realizing that
I'd written a similar problem) and then deleted the old version.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151//2006F/Labs/conditionals.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
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 Nov 30 21:42:58 2006.
The source to the document was last modified on Sat Sep 9 18:03:32 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/conditionals.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.