Haskell将类型字符转换为自定义类型字段
我试图使用查询显示表中的几列。但我收到了一条错误消息:Haskell将类型字符转换为自定义类型字段,haskell,type-mismatch,Haskell,Type Mismatch,我试图使用查询显示表中的几列。但我收到了一条错误消息: * Couldn't match type `Char' with `[Char]' Expected type: [[[Field]]] Actual type: [[[Char]]] * In the second argument of `mapMaybe', namely `(map transpose (table))' In the expression: mapMaybe (elemI
* Couldn't match type `Char' with `[Char]'
Expected type: [[[Field]]]
Actual type: [[[Char]]]
* In the second argument of `mapMaybe', namely
`(map transpose (table))'
In the expression:
mapMaybe (elemIndex columns) (map transpose (table))
In an equation for `project':
project columns table@(header : _)
= mapMaybe (elemIndex columns) (map transpose (table))
我的Haskell代码如下所示:
project columns table@(header : _)
= mapMaybe (elemIndex columns) (map transpose (table))
表格
的类型为表格
,即[行]
,或[[字段]]
字段
是字符串
的同义词让我们从查看我们正在使用的函数的签名开始:
elemIndex
具有以下签名:
elemIndex :: Eq a => a -> [a] -> Maybe Int
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
mappaye
具有以下签名:
elemIndex :: Eq a => a -> [a] -> Maybe Int
mapMaybe :: (a -> Maybe b) -> [a] -> [b]
然后,我们可以根据elemIndex
的参数columns
(根据名称,我希望它是[Field]
类型)专门化它的类型:
因此:
然后,根据它的第一个参数,elemIndex columns
,让我们对mappaye
进行同样的专门化:
mapMaybe :: ([[Field]] -> Maybe Int) -> [[[Field]]] -> Int
因此:
但根据你所说的,我认为表是表,你所说的是[行]
,是[[字段]]
,不能填补所需的[[字段]]
那么为什么会发生这种情况呢?我认为你误用了elemIndex
elemIndex
是一个函数,给定a
类型的值和[a]
类型的列表,查找列表中第一次出现的值的索引,并仅返回该值,如果该值根本不存在,则返回Nothing
:
elemIndex 5 [1, 2, 3, 4, 5]
> 4
但是,看起来您试图给它一个类型为a
的值和一个类型为[[a]]]
的列表,并获取第一个子列表的索引,以该值开头:
elemIndex' 5 [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]
> 4
标准库(或我所知道的任何库)中不存在此函数。我建议使用函数elemIndexBy
:
elemIndexBy :: (a -> Boolean) -> [a] -> Maybe Int
elemIndexBy fun list = go list 0
where go [] _ = Nothing
go (x:xs) ix
| fun x = Just ix
| otherwise = go xs $ succ ix
elemIndexBy ((5 ==) . head) [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]
> 4
字段
是字符串
的同义词吗?此外,您的错误消息与提供的代码不匹配。请仔细检查此代码产生的错误。您显示的代码与编译器错误消息中的代码不匹配。您应该提供一个复制错误的最小完整示例。否则我们猜不出有什么不对。Field是String Yes的同义词谢谢你的回答!你能举一个如何使用elemIndexBy的例子吗?我添加了一个用法示例。在函数类型的指导下,在Monad理解中自动使用自然转换的有趣案例:与中一样,跳过listtomabe[i |(x,i)中的listtomabe
elemIndexBy :: (a -> Boolean) -> [a] -> Maybe Int
elemIndexBy fun list = go list 0
where go [] _ = Nothing
go (x:xs) ix
| fun x = Just ix
| otherwise = go xs $ succ ix
elemIndexBy ((5 ==) . head) [[1, 5], [2, 4], [3, 3], [4, 2], [5, 1]]
> 4