Problem 13 (150pts): cond
Fill in the missing parts of do_cond_form
so that it correctly implements cond
(spec), returning the value of the first result sub-expression corresponding to a true predicate, or the result sub-expression corresponding to else
.
Some special cases:
- When the true predicate does not have a corresponding result sub-expression, return the predicate value.
- When a result sub-expression of a
cond
case has multiple expressions, evaluate them all and return the value of the last expression. (Hint: Useeval_all
.)
Your implementation should match the following examples and the additional tests in tests.scm.
scm> (cond ((= 4 3) 'nope)
((= 4 4) 'hi)
(else 'wait))
hi
scm> (cond ((= 4 3) 'wat)
((= 4 4))
(else 'hm))
#t
scm> (cond ((= 4 4) 'here (+ 40 2))
(else 'wat 0))
42
The value of a cond
is undefined
if there are no true predicates and no else
. In such a case, do_cond_form
should return None
. If there is only an else
, return its sub-expression. If it doesn't have one, return #t
.
scm> (cond (False 1) (False 2))
scm> (cond (else))
#t
Use Ok to unlock and test your code:
python ok -q 13 -u
python ok -q 13