Haskell Boolean Logic
Boolean logic combines Bool values into a single Bool result. It lets a program join several True or False facts into one answer.
And needs both sides to be True
The && operator joins True and False. One side is False, so the result is False.
main :: IO ()
main = print (True && False)Or needs only one True side
The || operator joins True and False. One side is True, so the result is True.
main :: IO ()
main = print (True || False)not flips a Bool value
The not operation flips True. The result is False.
main :: IO ()
main = print (not True)Example
main :: IO ()
main = print ((5 > 3) && (2 < 4))Key points
- Bool value: a value that is either True or False.
- And: the && operator gives True only when both sides are True.
- Or: the || operator gives True when at least one side is True.
- Not: the not operation flips True to False and False to True.
- Combined tests: comparisons give Bool values, so logic operators can join them.
In this example, both comparisons give True. The && operator joins them, and True joined with True gives True.
Note
not is a plain operation placed before its value, as in not True.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Haskell Playground
Exercise
What is the result of not (True && True)?