Haskell Comparison Operators
A comparison operator compares two values and gives a Bool result. The result is True when the comparison holds and False when it does not.
Equal values compare as True
The == operator compares the two numbers. They match, so the result is True.
main :: IO ()
main = print (4 == 4)Different values pass the not-equal test
The /= operator compares the two numbers. They differ, so the result is True.
main :: IO ()
main = print (4 /= 9)A smaller number passes the less-than test
The < operator compares the two numbers in order. Three is smaller than seven.
main :: IO ()
main = print (3 < 7)Text compares in alphabet order
The < operator also works on text. The word apple comes before banana in alphabet order.
main :: IO ()
main = print ("apple" < "banana")Example
main :: IO ()
main = print ('m' >= 'm')Key points
- Equal: the == operator asks whether two values are the same.
- Not equal: the /= operator asks whether two values differ.
- Less than: the < operator asks whether the left value is smaller.
- Greater than: the > operator asks whether the left value is bigger.
- At most and at least: <= means less than or equal, and >= means greater than or equal.
- Comparable values: comparisons work on numbers, characters and text.
In this example, the >= operator compares the two characters. They are equal, so the greater-than-or-equal test gives True.
Note
Haskell writes not-equal as /=, not as !=.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Haskell Playground
Exercise
What is the result of 10 > 12?