Definition with Guards
Functions can be defined with guards:
valAbs :: Int -> Int
-- returns the absolute value of an integer
valAbs n
| n >= 0 = n
| otherwise = -n
-
Guard evaluation is top-down and returns the result of the first true branch.
-
Pattern definitions can also have guards.
-
The
otherwiseis the same asTrue, but more readable
Equality goes after every guard!