显然,我的类型标识有误。我已经找到了问题所在。现在,我想更多地了解关于我的打字错误上的GHCI推断签名。我试图让这段代码正常运行:
elemNum :: (Eq a, Num b) => a -> [a] -> b
elemNum e l = f e l
where f _ [] = [] -- this was my typo, supposed to read 0
f e (x:xs)
| x == e = 1 + f e xs
| otherwise = f e xs
显然,由于上述原因,它无法工作;但是,如果我删除我的签名,则可以编译(不确定为什么,请解释一下),并且我会得到这个签名:
elemNum :: (Num [a], Eq t) => t -> [t] -> [a]
我以前从未见过类型类 Num [a]
,它是什么意思,它与 (Num a) => [a]
有什么区别。
[a]
设为Num
的成员吗?如果可以,你能提供一个简单的例子吗?如果不行,为什么? - Evan Carrollinstance Num [a] where fromInteger x = undefined; x + y = undefined
的方式使任何东西成为任何类型类的实例(前提是种类匹配)。然而,这显然是没有用的。 - John L