IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

HaskellのEitherとeither

Eitherという型がありますが、eitherという関数もあります。

Either型

Data.Either

Prelude> :i Either
data Either a b = Left a | Right b     -- Defined in `Data.Either'
instance (Eq a, Eq b) => Eq (Either a b)
  -- Defined in `Data.Either'
instance Monad (Either e) -- Defined in `Data.Either'
instance Functor (Either a) -- Defined in `Data.Either'
instance (Ord a, Ord b) => Ord (Either a b)
  -- Defined in `Data.Either'
instance (Read a, Read b) => Read (Either a b)
  -- Defined in `Data.Either'
instance (Show a, Show b) => Show (Either a b)
  -- Defined in `Data.Either'
either関数

Prelude

Prelude> :t either
either :: (a -> c) -> (b -> c) -> Either a b -> c
Prelude> 
実行例
Prelude> let m = (\x -> "yes")
Prelude> let n = (\x -> "no")
Prelude> either m n $ Left 1
"yes"
Prelude> either m n $ Right 2
"no"