我開始用haskell編程。我正在開發的程序只是將總數與兩個元素相加,例如:無法與實際類型`[([Char],a0)]'匹配'預期類型'([Char],t0)'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)]
這可能是非常簡單的,但作爲一個初學者我沒能解決這個問題。
謝謝,它現在可以工作(: – magamig