9 Booleans
If we’ve been through the previous part, we can keep using the same file. Or we can use 5-continuation-passing-style.rkt as our starting point.
Add booleans. Go blind.
9.1 Some tests
You might want to copy them into the test module at the bottom of your Racket file. The tests are going to fail at first. By the end of this chapter they should pass.
(check-equal? (evaluate '(if #f (+ 1 2) (+ 2 3))) 5)
(check-equal? (evaluate '(if (> 8 4) (+ 1 2) 0)) 3)
(check-equal? (evaluate '((λ (a b) (if (> a (+ b b)) (- a b) (+ a b))) 9 1)) 8)
(check-equal? (evaluate '((λ (a b) (if (> a (+ b b)) (- a b) (+ a b))) 9 5)) 14)
9.2 Literals
We want to have two boolean literals: #t (true) and #f (false). In eval-exp these can be matched and handled quite the same way as number-literals. We can use Racket’s boolean?-function to match booleans, the way we use the number?-function to match numbers.
9.3 if
[(list 'if exp then else) your code here]
9.4 Some functions
9.5 Maybe: and, or, ...
We can totally skip this part. It isn’t necessary for any of the stuff we will do later. But it’s maybe like nice or something.
We can implement stuff like and by matching on it in eval-exp and then kind of rewriting to an if-expression and evaluating that rewritten expression instead:
[(list 'and a b) (define rewritten-exp (list 'if your code here)) your code also here]