我需要帮助处理以下错误。基本上,对于给定的项目列表,我希望创建一个函数来检查项目是否在该列表中。为了做到这一点,我将列表转换为一个集合,然后在该集合周围创建一个闭包。
toAccept :: Ord a => [a] -> (a -> Bool)
toAccept xs =
let xset = Set.fromList xs in
let
-- accept:: Ord a => a -> Bool
accept x = Set.member xset x in
accept我得到的是
Could not deduce (a ~ Set.Set (Set.Set a))
from the context (Ord a)
bound by the type signature for
toAccept :: Ord a => [a] -> a -> Bool
at Tokens.hs:6:13-39
`a' is a rigid type variable bound by
the type signature for toAccept :: Ord a => [a] -> a -> Bool
at Tokens.hs:6:13
Expected type: a -> Bool
Actual type: Set.Set (Set.Set a) -> Bool
In the expression: accept
In the expression: let accept x = Set.member xset x in accept
In the expression:
let xset = Set.fromList xs in
let accept x = Set.member xset x in accept我做错了什么?
发布于 2013-10-30 16:09:17
你的论点失败了
let accept x = Set.member x xset in accept作为一项规则,在haskell函数中,数据结构排在最后,因为这使得它能够接受无点样式。有关此问题的更多信息,请参见here。
一种可能更好的写作方式是
toAccept' :: Ord a => [a] -> a -> Bool
toAccept' = flip Set.member . Set.fromList这里我们利用了Haskell的运行特性,因为[a] -> a -> Bool和[a] -> (a -> Bool)是相同的。实际上,我们可以编写函数,就像它接收两个参数而不使用一个参数一样,并返回一个函数,因为在Haskell中,这些选项是相同的。
https://stackoverflow.com/questions/19687943
复制相似问题