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
Before writing any code, unlock the tests to verify your understanding of the question:
$ python ok -q 13 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 13