2014-12-30 42 views
0
sumAllDigits :: [ Int ] -> Int 
sumAllDigits (x:xs) 
    |(x:xs) == [] = 0 
    |x >= 10 = sumDigits x + sumAllDigits xs 
    |x< 10 = x + sumAllDigits xs 

REPORT:
*递归> sumAllDigits [22,33] ***异常:Recursion.hs:(76,1) - (79,34):非穷尽在函数sumAllDigits非穷尽上的Haskell递归

+8

'x:xs'如何能等于'[]'? (提示:以同样的方式,1可以等于0.) – leftaroundabout

回答

1

我相信以下更改将为您纠正这种模式。我更愿意将它自己的实现与空白列表进行匹配。只是感觉对我更加明确。然后,因为x将通过>=下降,如果它小于,否则将覆盖这些情况。

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

看起来它可能会修复它,但如果你能解释什么是错误的以及如何解决这个问题,你的答案会更有用。 – icktoofay

+0

编辑这样做。对于那个很抱歉。在移动设备上,如果发生应用崩溃或连接丢失,请选择首先发布有用信息。 :) –

+2

“我更喜欢使空列表案例它自己的实现匹配” - 这确实是_in general_好建议,但在这里你可以更强烈地陈述它:如果'x:xs'是唯一的子句,函数_can绝不匹配空列表! – leftaroundabout