confusedandsad
confusedandsad

Reputation: 332

Haskell map on Right value of Either

I have a type Either a [b] and a function f :: (b -> c)

How can I use f on a value of type Either a [b] to get Either a [c] ?

Upvotes: 0

Views: 168

Answers (1)

AlexSchell
AlexSchell

Reputation: 1722

Use fmap function on it.

Either is an instance of the Functor typeclass with the first type fixed:

data Either a b = Left a | Right b

instance Functor (Either a)
  fmap _ (Left x)  = Left x
  famp f (Right y) = Right (f y)

and the fmap type for Either a therefore would be:

fmap :: (b -> c) -> Either a b -> Either a c

Appliction of fmap to Either leads to: Left data constructor of type a remains unchanged, and the Right data constructor of type b gets the function f applied to get to the type c.

Upvotes: 3

Related Questions