MultiWayIf
MultiWayIf allows us to create multiple cases for our if statements without nesting them. We define a sequence of expressions that evaluate to either True
or False
(conditions called guards) and associate an expression with each of them:
The symbol |
can be read as "such that..." or "where...". The guards are evaluated from top to bottom, and the expression associated with the first guard that is True
is chosen for further evaluation. The otherwise
function simply always evaluates to True
and the expression associated with it will always be further evaluated if none of the guards before it evaluates to True
. It gives us a convenient way to make sure that we have handled all possible cases. It is not necessary to add the otherwise
guard at the end, but if all the possible cases are not met, we will end up with an error at runtime.
One more thing we have to do in order to use the MultiWayIf
is to enable the extension in GHC. GHC has a number of special features that are disabled by default (like the MultiWayIf
) so we have to add a line above our module declaration in the following format to enable it:
Let's switch our trackScore
function to use MultiWayIf
instead, but not include the case where time == avg
. It will result in an error at runtime (not during compilation) because the case we entered has not been defined:
Let's fix it up:
This already reads much better than our first implementation with nested if-statements
, but there is a way to make it even nicer by using guarded equations.
Last updated