
On Tue, Apr 14, 2015 at 11:47 AM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
Hi,
Can someone please explain the difference in outputs of the following two expressions -
--------------
ghci> fmap (replicate 3) (Right "blah") Right ["blah","blah","blah"]
ghci> fmap (replicate 3) (Left "foo") Left "foo"
---------------
Why does 'Right' return a list of Strings whereas 'Left' returns just a String.
Because that's the way Either was made an instance of fmap. It's defined so that fmap _ (Left x) = Left x, but fmap f (Right y) = Right (f y). The docs say: The 'Either' type is sometimes used to represent a value which is either correct or an error; by convention, the 'Left' constructor is used to hold an error value and the 'Right' constructor is used to hold a correct value (mnemonic: \"right\" also means \"correct\").