isLeapYear : Int -> Bool
isLeapYear year =
let
divisibleBy number =
modBy number year == 0
in
divisibleBy 4 && (not (divisibleBy 100) || divisibleBy 400)
We can combine smaller logical expressions into larger ones using the logical operators &&
(and), ||
(or), and not
(negation).
A logicial expression is the most concise approach.
Precedence
In school they teach you that 2 + 3 * 4
is to be read as meaning 2 + (3 * 4)
.
This is a convention, chosen for its convenience.
We say that the *
operator has higher precedence than +
.
In logic similar ambiguities exist, and these are similarly resolved.
- and has higher precedence than or, and
- not has higher precedence than both and and or.
For example, p || q && r
means the same as p || (q && r)
.
If you don't wish to remember the precendence of all the things, or force readers of your code to do so, you can use parentheses to make it explicit.
13th Dec 2024
·
Found it useful?