0
sumAllDigits :: [ Int ] -> Int  
sumAllDigits (x:xs)  
   |(x:xs) == []  = 0  
   |x >= 10  = sumDigits x + sumAllDigits xs  
   |x< 10    = x + sumAllDigits xs  

REPORT:
*Recursion> sumAllDigits [22,33] *** Exception: Recursion.hs:(76,1)-(79,34): Non-exhaustive patterns in function sumAllDigits

overpro
  • 49
  • 8

1 Answers1

1

I believe the following changes will correct this for you. I prefer to make the empty list case it's own implementation to match against. Just feels more explicit to me. And then, since x will drop through the >= if it is less than, otherwise will cover those cases.

sumAllDigits :: [ Int ] -> Int  
sumAllDigits [] = 0
sumAllDigits (x:xs)  
   | x >= 10  = sumDigits x + sumAllDigits xs  
   | otherwise= x + sumAllDigits xs  
Andrew Monshizadeh
  • 1,784
  • 11
  • 16
  • 4
    That looks like it will probably fix it, but your answer would be more useful if you could explain what was wrong and how this remedies the problem. – icktoofay Dec 30 '14 at 00:50
  • Edited to do so. Sorry about that. On mobile and prefer to post helpful first in case of app crash or lost connection. :) – Andrew Monshizadeh Dec 30 '14 at 00:52
  • 2
    "I prefer to make the empty list case it's own implementation to match against" – this is indeed _in general_ good advice, but here you can state it rather more strongly: if `x:xs` is the only clause, the function _can never_ match an empty list! – leftaroundabout Dec 30 '14 at 00:55