[Haskell-beginners] Applicative instance for Either String

Michael Mossey mpm at alumni.caltech.edu
Sat Jul 17 07:14:56 EDT 2010


Oh I get it. I should use a type variable 's' instead of String.

 > instance Applicative (Either s) where
 >   pure x = Right x
 >   Right g <*> Right x = Right (g x)
 >   Right _ <*> Left s = Left s
 >   Left s <*> _ = Left s

I guess there is already a functor instance of Either s?

Interesting how functors and applicative functors can be thought of as 
containers, but one thing that makes them flexible is that they can contain 
zero, one, or many pieces of data (depending on the functor), and they can 
contain something of a different sort entirely (like Left).

Is it possible to make an applicative functor that contains auxiliary data 
unrelated to the "main" data. I think not, because then there's no way to 
define pure.

Michael Mossey wrote:
> Why does the following give me an error unless I enable FlexibleInstances?
> 
> instance Applicative (Either String) where
>   pure x = Right x
>   Right g <*> Right x = Right (g x)
>   Right _ <*> Left s = Left s
>   Left s <*> _ = Left s
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


More information about the Beginners mailing list