On this page:
9.1 Some tests
9.2 Literals
9.3 if
9.4 Some functions
9.5 Maybe:   and, or, ...
9.6 Done?
7.2

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

And we want some kind of if-then-else. In eval-exp we add a clause:

[(list 'if exp then else) your code here]

If it matches, we will evaluate the exp-expression, then choose then-expression or else-expression depending on the value we got, and then evaluate the expression we chose.

9.4 Some functions

And also we probably want some functions, like = and < and so on. Since our numbers are Racket-numbers and our booleans are Racket-booleans we can add them the same way we have added the other “primitives,” like + and - and such.
We will add at least =, <, <=, > and >=. We can add more later if we need more...

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]

9.6 Done?

Run and see that all the tests pass.
Next is Ambiguousness. We can keep using the Racket-file we’re working with, or skip to 6-booleans.rkt.