[Haskell-beginners] Left vs Right

Mike Meyer mwm at mired.org
Tue Apr 14 16:57:20 UTC 2015


On Tue, Apr 14, 2015 at 11:47 AM, Shishir Srivastava <
shishir.srivastava at 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\").
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150414/7dac6625/attachment.html>


More information about the Beginners mailing list