CS 135 Winter 2011
Brad Lushman
Assignment:
2
Due:
Wednesday, January 19, 9:30am
Language level:
Beginning Student
Files to submit:
cond.rkt
,
grades.rkt
,
risk.rkt
,
intervals.rkt
Warmup exercises:
HtDP 4.1.1, 4.1.2, 4.3.1, 4.3.2
Practise exercises:
HtDP 4.4.1, 4.4.3, 5.1.4
Policies from Assignment 1 carry forward. For example, your solutions must be entirely your own
work, and your solutions will be marked for both correctness and good style. Good style includes
qualities such as descriptive names, clear and consistent indentation, appropriate use of helper
functions, and documentation (design recipe).
For this and all subsequent assignments you are expected to use the design recipe as discussed in
class. You must use
checkexpect
for both examples and tests.
It is very important that the function names and parameters match ours. You must use the public
tests to be sure. The names of the functions will be given exactly. The names of the parameters
are up to you, but should be meaningful. The order and meaning of the parameters are carefully
specified in each problem.
Here are the assignment questions you need to submit.
1. A
cond
expression can always be rewritten to produce
equivalent expressions
. These are
new expressions that always produce the same answer as the original (given the same inputs,
of course). For example, the following are all equivalent:
(
cond
[(
>
x
0
)
’
One
]
[(
<
=
x
0
) ’
Two
])
(
cond
[(
<
=
x
0
) ’
Two
]
[(
>
x
0
)
’
One
])
(
cond
[(
>
x
0
) ’
One
]
[
else
’
Two
])
(There is one more really obvious equivalent expression; think about what it might be.)
So far all of the
cond
examples we’ve seen in class have followed the pattern
(
cond
[
question1 answer1
]
[
question2 answer2
]
. . .
[
questionk answerk
])
where
questionk
might be
else
.
The questions and answers do not need to be simple expressions like we’ve seen in class. In
particular, either the question or the answer (or both!) can themselves be
cond
expressions.
In this problem, you will practice manipulating these socalled “nested
cond
” expressions.
CS 135 — Winter 2011
Assignment 2
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Below are three functions whose bodies are nested
cond
expressions. You must write new
versions of these functions, each of which uses
exactly one cond
. Your versions must be
equivalent to the originals—they should always produce the same answers as the originals,
regardless of
x
or the definitions of the helper predicates.
(a)
(
define
(
q1a x
)
(
cond
[(
p1? x
)
(
cond
[(
p2? x
) ’
One
]
[
else
’
Two
])]
[
else
(
cond
[(
p2? x
) ’
Three
]
[
else
’
Four
])]))
(b)
(
define
(
q1b x
)
(
cond
[(
cond
[(
p1? x
)
true
]
[
else
(
p2? x
)])
’
One
]
[
else
’
Five
]))
(c)
(
define
(
q1c x
)
(
cond
[(
cond
[(
p1? x
) (
p2? x
)]
[
else
(
p2? x
)])
(
cond
[(
p3? x
) ’
Three
]
[
else
’
Four
])]
[
else
’
Five
]))
The functions
q1a
,
q1b
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '07
 VASIGA
 Semitone, Minor third, cavalry, cond

Click to edit the document details