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, Lisp has several comparison operators that allow us to set up comparison rules. Lisp 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 Lisp 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, highlight or double click the space where the answer should be.

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 (highlight to view the answer): False 

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 (highlight to view the answer): False 

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 7 6 8 9) is false!

Question: What would the following evaluate to?

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

Answer (highlight to view the answer): False 

Greater Than and Strictly Decreasing Order

The greater than operator (>) acts as a greater than comparison for two operands, or it ensures that items are in strictly decreasing order (each item is smaller than the one on the left). 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. 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: (< 9 8 7 5 6 4 3) is false!

Question: What would the following evaluate to?

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

Answer (highlight to view the answer): 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. Also, remember that order matters: (<= 3 4 5 7 6 8 9) is false!

Question: What would the following evaluate to?

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

Answer (highlight to view the answer): True 

Greater Than or Equal To and Decreasing Order

The greater than or equal to operator (>=) acts as a greater than or equal to comparison for two operands, or it ensures that items are in decreasing order (each item is smaller than or equal to the one on the left). 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. Like equality and inequality, this will only evaluate to true if the condition holds true for all of the operands. Also, remember that order matters: (<= 9 8 7 5 6 4 3) is false!

Question: What would the following evaluate to?

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

Answer (highlight to view the answer): 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, Lisp has provided three basic logical operations which allow us to create any combination of logical conditions that we want.

In the text, all caps are used when naming the operators to differentiate between using the operator name as an operator name and when using it as part of an English sentence. When using the operators in Lisp, the operators are always lowercase.

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. 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. (Please note, this does not check to see if the operands equal each other, it checks to see 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 is equal to 4 is true, even though 9 is less than 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. The NOT operator only takes one operand. Below is an example of an expression that evaluates to true:

(not (= 4 6))

Below is an example of an expression that evaluates to false:

(not (= 6 6))

The NOT operator inverts its operand, making the result be the opposite of the operand. So, if the operand evaluates to true (e.g. 6 = 6) then the not operator will make the whole expression evaluate to false.


How did we do?


Powered by HelpDocs (opens in a new tab)