【发布时间】:2014-08-30 21:20:41
【问题描述】:
我开始用haskell 编程。我正在开发的程序只是将一个包含两个元素的列表的总和相加,例如:
[("book",10),("cookies",2),("icecream",5)]
这应该返回“17”。这是我的代码:
total [] = []
total ([("c",e)]:y) = total y ++ [e]
但是在 GHCi 中运行时,它给了我这个错误:
<interactive>:80:8:
Couldn't match expected type `[([Char], a0)]'
with actual type `([Char], t0)'
In the expression: ("livro", 10)
In the first argument of `total', namely
`[("livro", 10), ("bolachas", 2), ("gelado", 5)]'
In the expression:
total [("livro", 10), ("bolachas", 2), ("gelado", 5)]
<interactive>:80:21:
Couldn't match expected type `[([Char], a0)]'
with actual type `([Char], t1)'
In the expression: ("bolachas", 2)
In the first argument of `total', namely
`[("livro", 10), ("bolachas", 2), ("gelado", 5)]'
In the expression:
total [("livro", 10), ("bolachas", 2), ("gelado", 5)]
<interactive>:80:36:
Couldn't match expected type `[([Char], a0)]'
with actual type `([Char], t2)'
In the expression: ("gelado", 5)
In the first argument of `total', namely
`[("livro", 10), ("bolachas", 2), ("gelado", 5)]'
In the expression:
total [("livro", 10), ("bolachas", 2), ("gelado", 5)]
这可能很简单,但作为一个初学者,我无法解决这个问题。
【问题讨论】: