Problem 12 (150pts): and
& or
Implement do_and_form
and do_or_form
so that and
and or expressions (spec) are evaluated correctly.
The logical forms and
and or
are short-circuiting.
For and
, your interpreter should evaluate each sub-expression from left to right, and if any of these is a false value, return that value.
Otherwise, return the value of the last sub-expression.
If there are no sub-expressions in an and
expression, it evaluates to #t
.
scm> (and)
#t
scm> (and 4 5 6) ; all operands are true values
6
scm> (and 4 5 (+ 3 3))
6
scm> (and #t #f 42 (/ 1 0)) ; short-circuiting behavior of and
#f
For the
and
andor
forms, remember to use our internal Python representations of#t
and#f
.
For or
, evaluate each sub-expression from left to right.
If any sub-expression evaluates to a true value, return that value.
Otherwise, return the value of the last sub-expression.
If there are no sub-expressions in an or
expression, it evaluates to #f
.
scm> (or)
#f
scm> (or 5 2 1) ; 5 is a true value
5
scm> (or #f (- 1 1) 1) ; 0 is a true value in Scheme
0
scm> (or 4 #t (/ 1 0)) ; short-circuiting behavior of or
4
Important: Use the provided Python functions is_scheme_true
and is_scheme_false
from scheme_utils.py
to test boolean values.
Before writing any code, unlock the tests to verify your understanding of the question:
$ python ok -q 12 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 12