【发布时间】:2010-10-23 08:09:06
【问题描述】:
我在这里有一个函数,用于查看元组列表并通过获取第一个值来查找元组中的第二个值。到目前为止的功能如下:
lookup :: String -> [(String,String)] -> String
lookup _ _ [] = "Not found"
lookup x y zs = if (notFound x zs)
then "Not found"
else (head [b | (a,b) <- zs, (a==x)])
如果没有包含给定第一个字符串的元组,notFound 函数只会返回一个 Bool 作为真。问题是,我在 Hugs 中遇到了这种类型的错误:
ERROR "find.hs" (line 22): Type error in explicitly typed binding
*** Term : lookup
*** Type : String -> [(String,String)] -> [a] -> String
*** Does not match : String -> [(String,String)] -> String
我认为这与虚拟“未找到”值与生成列表中的字符串具有不同类型有关,但我不确定。
【问题讨论】: