[Haskell-beginners] Question re "Pattern match(es) are non-exhaustive"
Joel Neely
joel.neely at gmail.com
Tue Feb 10 17:11:02 UTC 2015
Thanks for all the good explanations! I was misdirected by the fact that
the warning location was on the empty case, but based on descriptions I
think I understand why now.
I find it interesting that there is a bit of a conflict between "good
practice" heuristics:
1. Use -Wall (to eliminate questionable code that triggers warnings), and
2. Avoid _ in patterns in favor of explicit statement of the cases.
So it would appear that the advice (per Kim-Ee) to add an "otherwise" as a
third guard allows me to satisfy the compiler while documenting to the
human reader that the definition is really complete without it.
Thanks,
-jn-
On Tue, Feb 10, 2015 at 9:52 AM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:
>
> On Tue, Feb 10, 2015 at 7:58 PM, Joel Neely <joel.neely at gmail.com> wrote:
>
>> sumDigits (n:ns)
>> | n < 10 = n + sumDigits ns
>> | n >= 10 = r + sumDigits (q : ns)
>> where (q, r) = n `quotRem` 10
>>
>
> To reiterate what Mike and Brandon just said, it's not that both the []
> and (n:ns) cases have not been covered. They have.
>
> It's that the (n:ns) case hasn't been covered completely because of the
> guards.
>
> This will work:
>
> sumDigits (n:ns)
> | n < 10 = n + sumDigits ns
> | otherwise = r + sumDigits (q : ns)
>
> As will this:
>
> sumDigits (n:ns)
> | n < 10 = n + sumDigits ns
> | n >= 10 = r + sumDigits (q : ns)
> | otherwise = error "will never fire, only to suppress spurious
> warning"
>
> There's also -fno-warn-incomplete-patterns.
>
> -- Kim-Ee
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
Beauty of style and harmony and grace and good rhythm depend on simplicity.
- Plato
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150210/29ad73a2/attachment.html>
More information about the Beginners
mailing list