Just like we can group conditionals in imperative languages :
if(condicion) then
if(condicion) then accion
else accion
else accion
can we group guards in Haskell? I've tried, but it returns a parser error on the symbol "|" from the second guard (line two of the pseudocode):
| condicion
| condicion accion
| condicion accion
| otherwise accion
I've tried writing "=" before the grouping (just in case):
| condicion =
| condicion accion
| condicion accion
| otherwise accion
But neither. Can you help me? Thanks.
It is not possible to "nest" safeguards. All safeguards have to be referred to a logical expression:
It is used here
otherwise
for clarity, but it is still an alias oftrue
so that it works as the default case.For what you ask, the safeguard expression would have to be specialized:
Since the safeguards are evaluated in order, you can avoid evaluating the negative expression:
Anyway, you could always have used an
if..then
or statementcase..of
to do the same thing, it all depends on how complex the expressions are:Or also, use an auxiliary expression: