Computation Rules and Comparisons

General

There are times that we want to compare two or more values to see how they relate to each other. To do this, Clojure has several comparison operators that allow us to set up comparison rules. Clojure will then tell us if those rules hold true or not. It can be thought of as “Let me know if a condition holds true for these values.” The condition can be equality (“Let me know if 3 and 4 – 1 are equal”), less than (“Let me know if 8 / 2 is less than 9 / 1”), greater than (“Let me know if 4 * 5 is greater than 3 * 6”), etc.

Comparison operators function just like mathematical operators in that they can accept multiple operands and return one result. Additionally, there are Logical Operators which can be used to ensure that multiple comparison rules hold or don’t hold under specific conditions.

Basic Comparisons

The basic comparisons for Clojure are equality, inequality, greater than (or equal to), less than (or equal to), increasing order, and decreasing order. These are explained in detail below.

You’ll also find some practice problems. To reveal the answers, just click "Answer" below each example.

Equality

The equality comparison checks to see if all the operands are equivalent. These can be dates, numbers, text, etc. Below is an example of comparing the results of several equations to see if they are equal:

(= (* 3 4) (+ 4 8) (- 15 3))

Since all of the above equations evaluate to 12, the formula evaluates to true. This can be thought of as (* 3 4) = (+ 4 8) = (- 15 3) or 12 = 12 = 12. If we add the operand (+ 2 9) to the equation, then we would get false because it would be (* 3 4) = (+ 4 8) = (- 15 3) ≠ (+ 2 9), or 12 = 12 = 12 ≠ 11. Because there is an inequality, the whole expression becomes false.

Question: What would the following formula evaluate to?

(= 5 (- 9 4) (- (* 3 4) 7) (+ 6 2))

Answer
This formula would evaluate to False. Because the = operator sets all the operands as equal to each other, all three of the operands in parenthesis must be equal to five. Nine minus four is five, so the first one checks out. Three times four is twelve; twelve minus seven is five, so the second one also works. Six plus two? Not a chance. So, even though the first three operands are all equal to each other, the formula evaluates false because they are not equal to the fourth operand.

Inequality

The inequality comparison checks to see if all the operands are not equal. This can be dates, numbers, text, etc. Below is an example of comparing the results of several equations to see if they are not equal:

(not= (* 3 3) (+ 4 9) (- 15 5))

Since all of the above equations evaluate to different numbers, the expression evaluates to true. This can be thought of as (* 3 3) ≠ (+ 4 9) ≠ (- 15 5) or 9 ≠ 13 ≠ 10. If we add the operand (+ 3 7), then we would get false because it would be (* 3 3) ≠ (+ 4 9) ≠ (- 15 5) = (+ 3 7) or 9 ≠ 13 ≠ 10 = 10. Because there is an equality, the whole expression becomes false.

Question: What would the following evaluate to?

(not= 5 (- 9 4) (- (* 3 4) 7) (+ 6 2))

Answer
This formula would evaluate to False because the first three operands are equal to each other, even though they are not equal to the fourth operand. For a not= formula to evaluate as True, no operand can be equal to any other operand.

Less Than and Strictly Increasing Order

The less than operator (<) acts as a less than comparison for two operands, or it ensures that items are in strictly increasing order (each item is larger than the one on the left). For example, the formula (< 2 3) can be thought of to mean 2 < 3, and the formula (< 2 4 6) can be thought of to mean 2 < 4 < 6. Like equality and inequality, this will only evaluate to true if the condition holds true for all of the operands. Note that if two items are equal then the whole will be false. Also, remember that order matters: (< 3 4 5) is true, but (< 3 5 4) is false.

Question: What would the following evaluate to?

(< 4 7 9 (*3 4) (* 3 6))

Answer
This formula would evaluate as True. Each operand is less than the one to its right. Four is less than seven, seven is less than nine, nine is less than twelve, and twelve is less than eighteen.

Greater Than and Strictly Decreasing Order

The greater than operator (>) acts exactly like the less than operator (<), with the only difference being that now each operand must be greater than the one that follows. For example, the formula (> 4 1) can be thought of to mean 4 > 1, and the formula (> 9 6 2) can be thought of to mean 9 > 6 > 2. Again, it will only be true if it's true for all operands. And just like with the less than operator, it will be evaluate as false if any operand is equal to another.

Question: What would the following evaluate to?

(> 15 14 7 (*3 2) (* 2 3))

Answer
This formula would evaluate as False, and it's all because of that last operand. Fifteen is greater than fourteen, fourteen is greater than seven, and seven is greater than six. But the last two operands, (* 3 2) and (* 2 3), both equal six. Six isn't greater than six, so the whole formula is false.

Less Than or Equal To and Increasing Order

The less than or equal to operator (<=) acts as a less than or equal to comparison for two operands, or it ensures that items are in increasing order (each item is larger than or equal to the one on the left). For example, the formula (<= 2 2) can be thought of to mean 2 ≤ 2, and the formula (<= 2 4 6 6) can be thought of to mean 2 ≤ 4 ≤ 6 ≤ 6. Like equality and inequality, this will only evaluate to true if the condition holds true for all of the operands. The order matters here as well: (<= 3 4 5 4) would evaluate to false.

Question: What would the following evaluate to?

(<= 4 7 9 (*3 4) (* 2 6))

Answer
This formula evaluates to True. Four is less than seven, seven is less than nine, nine is less than twelve, and twelve is equal to twelve. Each operand is less than or equal to the one before it.

Greater Than or Equal To and Decreasing Order

In the same way that the greater than operator (>) mirrors the less than operator (<), the greater than or equal to operator (>=) works just like the less than or equal to operator (<=). It evaluates whether one item is greater than or equal to the next, which would mean that they decrease from left to right. For example, the formula (>= 4 1) can be thought of to mean 4 ≥ 1, and the formula (>= 9 6 2) can be thought of to mean 9 ≥ 6 ≥ 2. As with all the operator we've discussed, this will only hold true if it's true for every operand. So, if even one operand is less than the one to its right, the whole formula is false.

Question: What would the following evaluate to?

(>= 15 14 7 (*3 2) (* 2 3))

Answer
This formula evaluates to True. In the earlier example, we found that (> 15 14 7 (*3 2) (* 2 3)) was false because the last two operands are equal to each other. With the (>=) operator, however, those values can be equal to each other and the formula will still be true.

Logical Operations

Logical operations help us see if two or more comparisons evaluate a certain way. For example, maybe we want to know if 3 > 2 and 5 < 6, or we would like to know if 9 = 8 or 5 = 5. Because of these situations, Clojure has provided three basic logical operations which allow us to create any combination of logical conditions that we want.

In this article the operators and, or, and not are bolded when used as operators.

AND

The and operator checks to see if all comparisons hold. It can be thought of as “if x and y and z.” We can use the and operator like any other math operator, except we replace numbers with expressions that evaluate to true or false for the operands. (Please note, this does not check to see if the operands equal each other, it checks to see if the results of all the operands are true). Below is an example:

(and (= 4 4) (< 5 6))

The expression above evaluates to true because both of the formulas within parenthesis are true. However, the expression below evaluates to false, even though all of the operands are equivalent.

(and (= 5 9) (< 9 4) (> 2 3))

Below is another expression that evaluates to false, even though some of the operands are true statements.

(and (= 5 5) (< 4 9) (> 2 3))

OR

The or operator checks to see if at least one comparison holds. It can be thought of as “if x or y or z.” We can use the or operator like any other math operator, except we replace numbers with expressions that evaluate to true or false for the operands. (Like with and, this does not check to see if the operands equal each other, but if at least one of the operands is true). Below is an example:

(or (= 4 4) (< 9 6))

The above expression evaluates to true because (= 4 4) is true, even though (< 9 6) is false. Once an or operator finds at least one true statement, the whole expression becomes true regardless of other statements. Below is an example of an expression that evaluates to false.

(or (= 5 9) (< 9 4) (> 2 3))

NOT

The not operator checks to see if the given operand is not true. Unlike and and or, the not operator only takes one operand. Below is an example of an expression that evaluates to true:

(not (= 4 6))

It's a bit of a double negative: (= 4 6) is false, but (not (= 4 6)) is true. Below is an example of an expression that evaluates to false:

(not (= 6 6))

(= 6 6) is true, so (not (= 6 6)) is false. The not operator inverts its operand, making the result be the opposite of the operand.

There's a bit of overlap between the not operator and the not= operator from earlier in the article. For example both of these formulas would evaluate as true:

(not (= 2 4))

(not= 2 4)

The difference is that while not= can only be used to show that operands are not equal, not can be used to evaluate whether other formulas evaluate as true. For example:

(not (< 1 3 2)) would evaluate as true (because three is not less than two).

(not= (< 1 3 2)) on the other hand, is not valid Clojure syntax.


How did we do?


Powered by HelpDocs (opens in a new tab)