[Haskell-beginners] Question re "Pattern match(es) are non-exhaustive"

Kim-Ee Yeoh ky3 at atamo.com
Tue Feb 10 15:52:06 UTC 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150210/b602c6bc/attachment.html>


More information about the Beginners mailing list