Compare the pair
Boolean values commonly arise when you need to compare strings, numbers, or something else. Is the number 1 less than 2? Of course the expression is true. Haskell supports the usual comparison operators:
- Equal to,
==
. - Not equal to,
/=
.1 - Less than,
<
. - Less than or equal to,
<=
. - Greater than,
>
. - Greater than or equal to,
>=
.
Here are some mathematics facts because, I have been told, everyone loves facts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ghci> import Text.Printf
ghci> a = 1
ghci> b = 2
ghci> printf "a < b? %s\n" $ show $ a < b
a < b? True
ghci> printf "a > b? %s\n" $ show $ a > b
a > b? False
ghci> printf "a <= b? %s\n" $ show $ a <= b
a <= b? True
ghci> printf "a >= b? %s\n" $ show $ a >= b
a >= b? False
ghci> printf "a == b? %s\n" $ show $ a == b
a == b? False
ghci> printf "a /= b? %s\n" $ show $ a /= b
a /= b? True
The above operators can also be used to compare strings. Comparison is according to lexicographic order.
1
2
3
4
5
6
7
8
9
10
ghci> "a" < "b"
True
ghci> "a" > "b"
False
ghci> "A" < "a"
True
ghci> "A" == "a"
False
ghci> "A" /= "a"
True
The method compare
allows you to determine the ordering of two values. Unlike the comparison operators, the method compare
does not return a boolean value. Given two values that can be compared, the method compare
returns one of the following values.
LT
, meaning less than.EQ
, meaning equal.GT
, meaning greater than.
Exercises
Exercise 1. Why are the strings "Meow"
and "meow"
different? Within a GHCi session, convert either or both strings so the resulting strings compare equal according to ==
.
Exercise 2. Why are 'a'
and "a"
different? Within a GHCi session, convert either or both so the resulting values would compare equal according to ==
.
Exercise 3. Modify the program below so it would compile and the relevant values compare equal according to ==
.
1
2
3
4
5
6
7
import Text.Printf
-- | Same or different?
main = do
let a = 42
let b = "42"
printf "a == b? %s\n" $ show $ a == b
Exercise 4. The comparison operators also work with characters of the English alphabet. Just like the integers have an ordering to tell you that $1 < 2$ holds true, characters of the English alphabet have an ordering as well, i.e. alphabetic or lexicographic ordering. The letter “a” always comes before “b” and Haskell can confirm this dictionary ordering if you run the code "a" < "b"
. Here, the operator <
means “precede”. The expression "a" < "b"
can be read as: Does the letter “a” come before “b”? Note that an uppercase letter always precedes any lowercase letter. Consider the program below. Why does the expression "aardvark" < "Aardvark"
return False
? Modify the program so the result of the operator <
is True
.
1
2
3
4
5
6
7
8
9
10
11
12
import Text.Printf
-- | A small dictionary.
main = do
let a = "aardvark"
let b = "armadillo"
let c = "aardwolf"
let d = "Aardvark"
printf "b < a? %s\n" $ show $ b < a
printf "c < a? %s\n" $ show $ c < a
printf "b < c? %s\n" $ show $ b < c
printf "a < d? %s\n" $ show $ a < d
Exercise 5. A quiz has three statements, each of which evaluates to a boolean value. The statements are shown in the program below. Modify the program to output the number of correct statements, using only boolean arithmetics. Insert your code at the location shown in the program.
1
2
3
4
5
6
7
8
import Text.Printf
-- | How many correct solutions?
main = do
let resultA = 2 < 3
let resultB = 37 /= (read "37" :: Integer)
let resultC = 2 == 2.0
-- Insert code here.
Exercise 6. Translate the expression $1 < 2 < 3$ to Haskell code.
This is different from languages such as C, Java, and Python which use
!=
to compare for inequality. ↩