我开始用haskell编程.我正在开发的程序只是将一个列表的总和与两个元素相加,例如:
[("book",10),("cookies",2),("icecream",5)]
Run Code Online (Sandbox Code Playgroud)
这应该返回"17".这是我的代码:
total [] = []
total ([("c",e)]:y) = total y ++ [e]
Run Code Online (Sandbox Code Playgroud)
但是在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", …Run Code Online (Sandbox Code Playgroud) 我的课程是通过这样的输入来总结9级以上(从0到20)的学生数量:
aprov [("John",14)("Martha",8)("Elsa",12)]
Run Code Online (Sandbox Code Playgroud)
输出应为"2".但在编译时:
k = 0
aprov [] = 0
aprov ((a,n):y) = if n > 9 then k == k + 1 else k == k
Run Code Online (Sandbox Code Playgroud)
GHCi给了我这个错误:
Could not deduce (Num Bool) arising from the literal `0'
from the context (Num a, Ord a)
bound by the inferred type of
aprov :: (Num a, Ord a) => [(t, a)] -> Bool
at prog.hs:(29,1)-(30,55)
Possible fix: add an instance declaration for (Num Bool)
In the expression: 0
In an …Run Code Online (Sandbox Code Playgroud)