我是Purescript的新手(以及Haskell),并且陷入无法统一的错误。最初我有:
newtype Domain = Domain String
newtype Keyword = Keyword String
type Result = {
domain :: Domain,
occurred :: Boolean,
position :: Number,
quality :: Number
}
is_min_pos :: Maybe Result -> Maybe Result -> Maybe Result
is_min_pos Nothing Nothing = Nothing
is_min_pos Nothing y = y
is_min_pos x Nothing = x
is_min_pos x y = if y.position < x.position then y else x
这给了我错误
Cannot unify type
Prim.Object
with type
Data.Maybe.Maybe
我以为是因为它期望x和y属于Maybe Record类型。因此,为了明确起见,我将代码更改为按类型进行模式匹配。
data Result = Result {
domain :: Domain,
occurred :: Boolean,
position :: Number,
quality :: Number
}
is_min_pos (Result x) (Result y) = if y.position < x.position then y else x
现在我得到了错误
Cannot unify type
Data.Maybe.Maybe Processor.Result
with type
Processor.Result
这是指本节
y.position < x.position -- in the first case
在第二种情况下
Result x -- on the pattern matching side
我正在进一步研究
type Results = List Result
get_item_with_min_position :: Results -> Maybe Result
--get_item_with_min_position [] = Nothing
get_item_with_min_position results = foldl is_min_pos Nothing results
我正在使用Foldable中的“ foldl”。我不确定如何模式匹配一个空列表。如果可以,我可以将类型签名更改为
is_min_pos :: Maybe Result -> Result -> Maybe Result
我现在得到了错误
Cannot unify type
Prim.Object
with type
Data.Maybe.Maybe
这是可以理解的,因为
foldl is_min_pos Nothing results
结果是列表结果类型is_min_pos期望可能是结果
有什么干净的方法可以解决这个问题?
该Maybe
类型具有两个数据构造函数:Nothing
,您正确匹配的和Just
。如果你想匹配类型的东西Maybe a
它确实包含一个值,你应该匹配的Just
构造函数。
您需要修改最终情况,如下所示:
is_min_pos (Just x) (Just y) = if y.position < x.position
then Just y
else Just x
在这里,Just x
具有type Maybe Result
,根据类型签名是正确的,因此x
具有type Result
,因此您可以使用.position
访问器读取其position
属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句