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

Joel Neely joel.neely at gmail.com
Tue Feb 10 12:58:37 UTC 2015


I understand the meaning of the warning, but got it in a situation I didn't
expect. The following source file contains the simplest case I could
construct quickly to illustrate my question:

Why does the first function (sumDigits) get the "non-exhaustive" warning?
It contains a definition for both empty and non-empty arguments, just as
the second (sumList), which does not get a warning.

{-# OPTIONS_GHC -Wall #-}

sumDigits :: [Integer] -> Integer
sumDigits []     = 0
sumDigits (n:ns)
  | n < 10       = n + sumDigits ns
  | n >= 10      = r + sumDigits (q : ns)
  where (q, r)   = n `quotRem` 10

sumList :: [Integer] -> Integer
sumList []     = 0
sumList (n:ns) = n + sumList ns


​For completeness, here is the ghci transcript, with the location reported:

Prelude> :load sumDigits.hs

[1 of 1] Compiling Main             ( sumDigits.hs, interpreted )


sumDigits.hs:4:1: Warning:

    Pattern match(es) are non-exhaustive

    In an equation for ‘sumDigits’: Patterns not matched: _ : _

Ok, modules loaded: Main.

​
​Thanks in advance for any guidance on this.
-jn-​


-- 
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/f89f920d/attachment.html>


More information about the Beginners mailing list