[Haskell-beginners] Left vs Right

Alex Hammel ahammel87 at gmail.com
Tue Apr 14 17:29:07 UTC 2015


If you really do want to apply a function over data in a Left, there's
always `Control.Arrow.left`, which is the complement of `fmap` in this case:

λ left (replicate 3) (Left "blah")
Left ["blah","blah","blah"]
λ left (replicate 3) (Right "blah")
Right "blah"

Or you can use Either's Bifunctor instance:

λ import qualified Data.Bifunctor as Bi
λ Bi.first reverse (Left "foo")
Left "oof"
λ Bi.first reverse (Right "bar")
Right "bar"
λ Bi.second (intersperse '!') (Left "papaya")
Left "papaya"
λ Bi.second (intersperse '!') (Right "banana")
Right "b!a!n!a!n!a"
λ Bi.bimap sort reverse (Left "hello")
Left "ehllo"
λ Bi.bimap sort reverse (Right "goodbye")
Right "eybdoog"

On Tue, Apr 14, 2015 at 9:58 AM, Chas Leichner <chas at chas.io> wrote:

> This is meant to model errors. You want your computation to continue if
> everything is going all-Right, but if there is an error, you want it to
> stop there are report the error. This error is represented by Left
> constructor.
>
> On Tue, Apr 14, 2015 at 9:56 AM, Bob Ippolito <bob at redivi.com> wrote:
>
>> The Functor instance is defined for `Either a`, so Left is fixed. fmap
>> only maps over Right. You'll find the same behavior for `(,) a`, e.g. `fmap
>> (*2) (1,2) == (1,4)`.
>>
>> On Tue, Apr 14, 2015 at 9: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.
>>>
>>> Thanks,
>>> Shishir
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150414/c6421b11/attachment-0001.html>


More information about the Beginners mailing list