パターンの合わせ方
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)次のパターンに一致します。
NothingJust (Left (x,y))とはどこxですかIntyStringJust (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 -}