Lesson 5. Arithmetic comparisons
Arithmetic comparisons in Clojure follow the same syntax as arithmetic operations where the symbol comes before the numbers and the expression is wrapped into parenthesis.
After completing this lesson, you will be able to read and write Clojure expressions for:
- comparing numbers
- comparing arithmetic expressions
- checking whether a number is positive or negative
- checking whether an arithmetic expression is positive or negative
5.1 Equality check
In many languages, the equal sign is used for two kind of operations:
- assignment of a value into a variable, usually with a single equal sign =
- equality check between values, usually with two equal signs ==
For instance in python, variable assignment is done wit a single equal sign:
num = 1
And equality check is done via the == symbol:
2 == 3
(In javascript, we even have the === symbol, which behaves slightly differently from the == symbol.)
In Clojure, assignment and equality check are well separated by completely different symbols. The = symbol is only for quality check. Variable assignment is done with the def form that will be explored in Lesson 11.
Checking whether two numbers are equal is written like this:
(= 2 3)
The syntax for equality check is exactly the same as the syntax for arithmetic operations that we introduced in the previous lesson. The difference is that an expression like (= 2 3) returns true or false, while an expression like (+ 2 3) returns a number.