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:
==
./=
.1<
.<=
.>
.>=
.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.:exercise:
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:
Why are 'a'
and "a"
different? Within a GHCi session, convert either or both
so the resulting values would compare equal according to ==
.
:exercise:
Modify the program below so it would compile and the relevant values compare
equal according to ==
.
:include: file=”assets/src/data/same.hs”, line=25:-
:exercise:
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
.
:include: file=”assets/src/data/dictionary.hs”, line=25:-
:exercise: 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.
:include: file=”assets/src/data/quiz.hs”, line=25:-
:exercise: 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. ↩