I’ve created a function in which I can use (as far as I know) case expressions or guards.
foo a b c = case a of 1 -> [...]
2 -> [...]
3 -> [...]
[...]
otherwise -> error "..."
or
foo a b c | a == 1 = [...]
| a == 2 = [...]
| a == 3 = [...]
| [...]
| otherwise = error "..."
So, the question is: which of those 2 (case or guards) are “better” coding? Are both basically the same?
The first one is considered better style, for 2 reasons.
First of all: Many people would say that it looks better, since you don’t have to type out all of the
==. This is a very subjective reason, of course. Also, you would normally not even introduce a new case statement, but just match the arguments in the function argument list like so:This makes the code even more compact and readable, which many people like.
Secondly, there actually is a semantic difference. Imagine that you have a data type like this:
If you use the first method, you match against the constructors in the
case..ofexpression:However, if you try to do a guarded expression of some sort, like this:
… it won’t actually work, because the
Caketype doesn’t have anEqinstance, so you can’t use==to compare two values. Introducing anEqinstance (withderiving (Eq)after the data definition) is not always wanted, so not having to do it in this case might be significant.