In defining multiple pattern matches for a function, for instance as follows:
1: takeTree 0 tree = Leaf
2: takeTree levels (Leaf) = Leaf
3: takeTree levels (Branch value left right) = Branch value (takeTree...
I get two warnings in particular:
Source.hs:1: Warning: Defined but not used: `tree’
Source.hs:2: Warning: Defined but not used: `levels’
I’m not immediately convinced these are useful warnings though. If my code were instead:
1: takeTree 0 _ = Leaf
2: takeTree _ (Leaf) = Leaf
3: takeTree levels (Branch value left right) = Branch value (takeTree...
which, fixes the warnings, I now find it to be far less readable, and obfuscates the semantics of what I expect as input values.
Why is Defined but not used a reasonable warning at all here, when among my exhaustive patterns each argument is in fact used at least once?
I’ve made coding errors that were pointed out by this warning. Simplified example:
The recursive call should have
xs'as argument, of course, and the “defined but not used” warning will catch that. To me, that’s worth the inconvenience of using _ for unused matches.The compiler is not able to guess your intentions, and the fact that you used the argument in another match does not mean that you didn’t mean to use it in the match that generates the warning. After all, you could have used
tree, so the warning that you didn’t is reasonable.See also the answer by Ben: you can use
_nameto use a name but still suppress the warning.