パターンの合わせ方
Maybe (Either (Int, String) String)
私はそのような種類の入力で関数を書く必要があり、そのような入力をどのように解析することができますか?
パターンの合わせ方
Maybe (Either (Int, String) String)
私はそのような種類の入力で関数を書く必要があり、そのような入力をどのように解析することができますか?
タイプにはMaybe a
パターンJust a
とNothing
. タイプにはEither a b
パターンLeft a
とRight b
. したがって、 type の値はMaybe (Either (Int, String) String)
次のパターンに一致します。
Nothing
Just (Left (x,y))
とはどこx
ですかInt
y
String
Just (Right z)
はどこz
ですかString
。f :: Maybe (Either (Int, String) String) -> <SOMETHING>
f x = case x of
Just (Left (i, s)) -> <...>
Just (Right s) -> <...>
Nothing -> <...>
matchme Nothing = "Nothing"
matchme (Just (Left (x,y)) = "Left " ++ show x ++ " " + y
matchme (Just (Right z)) = "Right " ++ z
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 -}