我是 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
我正在使用可折叠的“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 期望可能结果
什么是解决这个问题的干净方法?