我有一个类型定义为这样:'x'被定义为'y',但编译器无法将'y'与'x'匹配,为什么?
data People = People [[String]]
和看起来像这样一个函数定义:
myFunction :: ([String], People) -> (Int, Int) -> People
myFunction first_tuple second_tuple = filter (myPredicate (fst second_tuple) (fst first_tuple)) (snd first_tuple)
这给了我下面的错误:
error:
* Couldn't match expected type `[[String]]'
with actual type `People'
* In the second argument of `filter', namely `(snd first_tuple)'
这有我真的难倒,因为我试图尽可能明确与我的括号和People
是明确的y定义为[[String]]
。是什么导致这种情况,为什么是这种情况?
这里是myPredicate
类型签名:
myPredicate :: Int -> [String] -> [String] -> Bool
请包括'myPredicate'的定义 - 现在你的代码也包含多个错误('MyFunction'应该是'myFunction'),如果你正在通过'[[String]]过滤'People',你需要通过应用构造函数将后者转换为'People'。 – epsilonhalbe
'人'是用'[[String]]来定义的,但不同于'[[String]]',也不能互换。它绝对没有被定义为** ** [[String]]'。 –
@epsilonhalbe我编辑了我的帖子以包含'MyPredicate'的类型定义。 – JavascriptLoser