-2

パターンの合わせ方

Maybe (Either (Int, String) String)

私はそのような種類の入力で関数を書く必要があり、そのような入力をどのように解析することができますか?

4

4 に答える 4

7

タイプにはMaybe aパターンJust aNothing. タイプにはEither a bパターンLeft aRight b. したがって、 type の値はMaybe (Either (Int, String) String)次のパターンに一致します。

  • Nothing
  • Just (Left (x,y))とはどこxですかIntyString
  • Just (Right z)はどこzですかString
于 2013-02-05T09:36:20.890 に答える
3
f :: Maybe (Either (Int, String) String) -> <SOMETHING>
f x = case x of
    Just (Left (i, s)) -> <...>
    Just (Right s) -> <...>
    Nothing -> <...>
于 2013-02-05T09:36:26.837 に答える
2
matchme Nothing = "Nothing"
matchme (Just (Left (x,y)) = "Left " ++ show x ++ " " + y
matchme (Just (Right z)) = "Right " ++ z
于 2013-02-05T09:38:12.167 に答える
2

maybe次のようにandeither関数を使用することもできます。

matchit = maybe nothing (left `either` right)
  where
      nothing = {- value for the nothing case -}
      left (x,y) = {- code for the (Just (Left (x,y)) case -}
      right z = {- code for the (Just (Right z)) case -}
于 2013-02-05T10:33:29.073 に答える