Classic way to define Haskell functions is
f1 :: String -> Int
f1 ('-' : cs) -> f1 cs + 1
f1 _ = 0
I’m kinda unsatisfied writing function name at every line. Now I usually write in the following way, using pattern guards extension and consider it more readable and modification friendly:
f2 :: String -> Int
f2 s
| '-' : cs <- s = f2 cs + 1
| otherwise = 0
Do you think that second example is more readable, modifiable and elegant? What about generated code? (Haven’t time to see desugared output yet, sorry!). What are cons? The only I see is extension usage.
I prefer writing function name when I am pattern matching on something as is shown in your case. I find it more readable.
I prefer using guards when I have some conditions on the function arguments, which helps avoiding
if else, which I would have to use if I was to follow the first pattern.So to answer your questions
No, I prefer the first one which is simple and readable. But more or less it depends on your personal taste.
I dont think there will be any difference in the generated code. Both are just patternmatching.
Well patternguards are useful to patternmatch instead of using let or something more cleanly.
Well the con is ofcourse you need to use an extension and also it is not Haskell98 (which you might not consider much of a con)
On the other hand for trivial pattern matching on function arguments I will just use the first method, which is simple and readable.