【发布时间】:2018-10-09 13:02:15
【问题描述】:
我尝试编写一个函数 [int] -> int 用迭代函数计算整数列表的总和(结果应该等于内置函数 sum)
19>sumList :: [Int] -> Int
20>sumList [list] | length[list] > 0 = [list]!!0 + sumList (drop 1 [list])
21> | otherwise = 0
这是我尝试运行的结果
uebung1.lhs:20:2: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for ‘sumList’:
Patterns not matched:
[]
(_:_:_)
Ok, modules loaded: Main.
*Main> sumList []
*** Exception: uebung1.lhs:(20,2)-(21,31): Non-exhaustive patterns in function sumList
*Main> sumList [3]
*** Exception: uebung1.lhs:(20,2)-(21,31): Non-exhaustive patterns in function sumListi i i i i
我做错了什么?我已经睡了一个晚上,但我只是不明白问题出在哪里。受保护的方程应该捕获所有列表长度的情况。感谢您的建议。
【问题讨论】:
-
这不是递归的吗?
-
是的,确实修复了它