Comparing Numbers in Math Class
In math class, we write expressions to compare numbers and variables. Here are some examples:
3 < 7
x > 5
a ≤ b
Comparing Numbers or Strings in Racket
Exploration 4.1 - Comparing Numbers
Enter the following functions in the Interactions Window and learn what values are produced. Record the results on page 71 of your workbook.
(< 3 7)
(> 3 7)
(>= 4 3)
(<= −2 3)
(<= 4 4)
(< 4 4)
(define x 8)
(< x 4)
(< 4 10)
Functions that Compare Numbers
Each of these functions consume two numbers and produce either true or false.
Function | Produces |
< | true if the first number is strictly less than the second number; otherwise false |
<= | true if the first number is less than or equal to the second number; otherwise false |
> | true if the first number is strictly greater than the second number; otherwise false |
>= | true if the first number is greater than or equal to the second number; otherwise false |
= | true if the first number is equal to the second number; otherwise false |
Comparing Strings
The following function consumes two strings and produces either true or false
string=? | true if the first string is identical to the second string; otherwise false |
Examples:
(string=? "apple" "orange") produces false.
(string=? "apple" "Apple") produces false.
(string=? "apple" "apple") produces true.
Exploration 4.2 - Comparing Strings
Enter the following functions in the Interactions Window and learn what values are produced. Record the results on page 71 of your workbook.
(string=? "Apple" "Apple")
(string=? "Apple" "apple")
(string=? "bananna" "orange")
(string<? "bananna" "orange")
(string<? "a" "b")
(string-length "raisin")
(> (string-length "raisin") (string-length "apple"))
(define fruit "mellon")
(string=? fruit "mellon")
The Boolean Data Type
The Boolean data type is named after a British mathematician, George Boole, who studied logic in the 1800's. The Boolean data type has only two possible values: true or false.
Boolean Functions
These functions consume two or more Boolean values and produce a Boolean value.
Function | Produces |
and | true if all arguments are true; otherwise false |
or | true if any one argument is true; otherwise false |
Exploration 4.3 - Boolean Functions
Enter the following functions in the Interactions Window and learn what values are produced. Record the results on page 72 of your workbook.
(and true true)
(and true false)
(and false true)
(and true true true)
(and true true false)
(or true true)
(or true false)
(or false false)
(or false false false false true)
Compound Comparisons
We often use Boolean functions to perform compound comparisons. These are comparisons that test two or more conditions.
Exploration 4.4 - Compound Comparisons
Draw the circle of evaluation for each of the following functions on page 72 of your workbook. Then enter the function in the Interactions Window and learn what values are produced. Record the results on page 72.
(and (< 1 3) (< 3 5))
(or (< 3 2) (< 3 5))
(define y 3)
(and (< y 6) (< 1 y))
(and (string=? "apple" "apple") (< 3 5))
(define fruit "melon")
(and (string<? fruit "pear") (> (string-length fruit) (string-length "pear")))