3

私は 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

そして2番目のケースでは

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

結果は List Result 型です is_min_pos は Maybe Result を期待します

これを解決するためのクリーンな方法は何でしょうか?

4

1 に答える 1

5

型には 2 つの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は、Maybe Result型シグネチャによると正しいtype があり、 xtypeもあるため、アクセサーをResult使用してそのプロパティを読み取ることができます。.positionposition

于 2015-05-04T16:47:58.273 に答える