Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Left vs Right (Shishir Srivastava)
2. Re: Left vs Right (Bob Ippolito)
3. Re: Left vs Right (Mike Meyer)
4. Re: Left vs Right (Chas Leichner)
5. Re: Left vs Right (Alex Hammel)
----------------------------------------------------------------------
Message: 1
Date: Tue, 14 Apr 2015 17:47:33 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Left vs Right
Message-ID:
<CALe5RTsEUz6J0o=Ls10Brpamo2=sunsjrgwuxscl9g8eff6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150414/f0cdb84c/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 14 Apr 2015 09:56:48 -0700
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Left vs Right
Message-ID:
<cacwmpm8scwf7v9smzcdof3ba+upvstmmmekgqtpe9xf8gh8...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <
[email protected]> 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
> [email protected]
> 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/0cc1a447/attachment-0001.html>
------------------------------
Message: 3
Date: Tue, 14 Apr 2015 11:57:20 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Left vs Right
Message-ID:
<CAD=7U2Bm=vjeqntf3qd7xgdtogy8f9j91vrywpcb3zh-g3x...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, Apr 14, 2015 at 11:47 AM, Shishir Srivastava <
[email protected]> 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-0001.html>
------------------------------
Message: 4
Date: Tue, 14 Apr 2015 09:58:16 -0700
From: Chas Leichner <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Left vs Right
Message-ID:
<CALPPNryYr8jQUO95uemLjauW_FDh=r5y2c4_nqfkrjpakgc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> 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 <
> [email protected]> 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
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> 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/96a64453/attachment-0001.html>
------------------------------
Message: 5
Date: Tue, 14 Apr 2015 10:29:07 -0700
From: Alex Hammel <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Left vs Right
Message-ID:
<CA+_xFeoN4toun0bSRQOqySVxeke4LDR2nH3rp+DOQOw92c0=u...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> 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 <[email protected]> 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 <
>> [email protected]> 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
>>> [email protected]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> 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.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 82, Issue 14
*****************************************