What is the difference between MultiWayIf and classic guards?

  • A+
Category:Languages

What does MultiWayIf extension offer that cannot be done using | guards and vice versa?

For instance, one can write same function as

f x | x == 0 = -1     | otherwise = x 

or

f x = if | x == 0 -> -1          | otherwise -> x 

I don't see any difference between these two examples beside syntactic sugar used. Are there situations where I should prefer one over another?

 


Multiway if is not supposed to be used instead of function guards. If you can use guards, you should do that. But the multiway if can also be used in the middle of an expression, much like case, e.g.:

f x = "The argument is " ++           if | x > 0 -> "positive"             | x < 0 -> "negative"             | otherwise -> "zero" 

Personally I do find the clarity of this specific example rather questionable, but this is the difference.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: