Lesson 7. Complex nested expressions

 

At this point, your mind starts to get used to the wrapping parenthesis in Clojure: simple compound expressions like (+ 2 3), (* 3 4 5) and (or true false) don’t feel as weird as they felt when you start to read this book.

But what about complex nested expressions like (or (and (> 3 2) (zero? 2)) (not (= 2 3)))? It probably still makes you a bit of discomfort. That’s perfectly fine. The purpose of this Lesson that concludes Unit 1 is to make you feel more comfortable with complex nested expressions.

After reading this lesson, you will be able to:

  • Visualize a complex nested expression in a tree diagram
  • Describe the structure of a complex nested expression

7.1   Visualization

Now that our logical operations portfolio is complete, we can write some complex boolean expressions. Once you understand the mechanics of the evaluation of a complex boolean expression, you will be able to understand the mechanics of the evaluation of any Clojure expression.

Let’s inspect a nested expression that combines and, or and not.

(or (and (> 3 2) (zero? 2)) (not (= 2 3)))

This expression looks a bit scary. Let’s reformat it, by having each form argument in a separate line:

(or (and (> 3 2)
          (zero? 2))
     (not (= 2 3)))

When you develop in a Clojure environment, the auto formatting (also called smart indentation) is done by the editor. In Unit 2, we will see how the REPL handles auto formatting.

7.2   Exercises

7.2.1   Ex 1:

7.2.2   Ex 2:

7.3   Summary