Left vs Right

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Left vs Right

Shishir Srivastava
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.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Bob Ippolito
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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150414/0cc1a447/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Mike Meyer
In reply to this post by Shishir Srivastava
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>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Chas Leichner
In reply to this post by Bob Ippolito
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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150414/96a64453/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Alex Hammel
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>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Kostiantyn Rybnikov
In reply to this post by Shishir Srivastava
Adding to others,

For fmapping left value I suggest using fmapLeft from errors package, for
fmapping both you can use

either (Left . replicate 3) (Right . replicate 3)
14 ????. 2015 19:48 "Shishir Srivastava" <shishir.srivastava at gmail.com>
????:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150415/ebfc14eb/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

emacstheviking
I just love the way Haskell can blow something really simple into something
truly mind blowingly obtuse looking at times!
It's no wonder Haskell won't be "mainstream" for a while yet, but that's
fine, it means the rest of us can charge better rates for now.
:)


On 15 April 2015 at 13:50, Kostiantyn Rybnikov <k-bx at k-bx.com> wrote:

> Adding to others,
>
> For fmapping left value I suggest using fmapLeft from errors package, for
> fmapping both you can use
>
> either (Left . replicate 3) (Right . replicate 3)
> 14 ????. 2015 19:48 "Shishir Srivastava" <shishir.srivastava at gmail.com>
> ????:
>
>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150415/7d100fd8/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Kostiantyn Rybnikov
You can still omit monads and keep pair of values in tuple and work with
them easily, but I understand the point you are making.

But I should note that migration from PHP to Python also takes first
impression of complication of many things, where instead of having "just
index.php" with everything inlined you would have some wsgi protocols, wsgi
servers etc, but later you understand that this was "the right thing". Same
with Haskell, it does pay off a lot in future :)
15 ????. 2015 19:27 "emacstheviking" <objitsu at gmail.com> ????:

> I just love the way Haskell can blow something really simple into
> something truly mind blowingly obtuse looking at times!
> It's no wonder Haskell won't be "mainstream" for a while yet, but that's
> fine, it means the rest of us can charge better rates for now.
> :)
>
>
> On 15 April 2015 at 13:50, Kostiantyn Rybnikov <k-bx at k-bx.com> wrote:
>
>> Adding to others,
>>
>> For fmapping left value I suggest using fmapLeft from errors package, for
>> fmapping both you can use
>>
>> either (Left . replicate 3) (Right . replicate 3)
>> 14 ????. 2015 19:48 "Shishir Srivastava" <shishir.srivastava at gmail.com>
>> ????:
>>
>>> 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/20150415/5fdac0aa/attachment-0001.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Chaddaï Fouché
>
> 15 ????. 2015 19:27 "emacstheviking" <objitsu at gmail.com> ????:
>
> I just love the way Haskell can blow something really simple into
>> something truly mind blowingly obtuse looking at times!
>> It's no wonder Haskell won't be "mainstream" for a while yet, but that's
>> fine, it means the rest of us can charge better rates for now.
>> :)
>>
>>
This is simple. More precisely, it couldn't be otherwise.

data Either l r = Left l | Right r

See how this definition has two type parameters ? The problem was obscured
in the first question by the fact that "replicate 3" can be applied to any
type. But if you take a less polymorphic function like length, it soon
become obvious that this is the way fmap should work :

fmap length (Right "stuff") = length "stuff"
fmap length (Left 5)          = length 5 ???? What does that even means ?

Since the type contained by Left and Right are different, you can't in
general apply the same function to both possibilities. Now the right type
is the last one to occur in "Either left right" so it's the one that's
abstracted over in the Functor instance, there is no Functor instance for
Either, the instance is for "Either left" with the left type fixed, fmap
don't touch the left type, so it can't do anything to a "Left x" value.

Semantically, "Either left right" can be seen as a container of one or zero
"right" that contains out-of-band information of type "left" when it's
empty. So when you fmap over it, you only touch the content, not the
out-of-band information.

--
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150415/c982d96c/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

emacstheviking
"Semantically, "Either left right" can be seen as a container of one or
zero "right" that contains out-of-band information of type "left" when it's
empty. So when you fmap over it, you only touch the content, not the
out-of-band information."

Yup. Whatever he said. ;)  ...that's bordering on monadic comparison
tutorials if you ask me... LOL. A lot of people may not even know what "in
band" and "out-of-band" even means. I grew up with X25, RS232, HDLC etc so
that's ok by me. You have to be very very careful about how you explain
things to  people. One of my favourite videos on YouTube is some guy asking
Richard Feynman how magnets work...well, he did ask!
https://www.youtube.com/watch?v=wMFPe-DwULM



On 15 April 2015 at 20:39, Chadda? Fouch? <chaddai.fouche at gmail.com> wrote:

> 15 ????. 2015 19:27 "emacstheviking" <objitsu at gmail.com> ????:
>>
>> I just love the way Haskell can blow something really simple into
>>> something truly mind blowingly obtuse looking at times!
>>> It's no wonder Haskell won't be "mainstream" for a while yet, but that's
>>> fine, it means the rest of us can charge better rates for now.
>>> :)
>>>
>>>
> This is simple. More precisely, it couldn't be otherwise.
>
> data Either l r = Left l | Right r
>
> See how this definition has two type parameters ? The problem was obscured
> in the first question by the fact that "replicate 3" can be applied to any
> type. But if you take a less polymorphic function like length, it soon
> become obvious that this is the way fmap should work :
>
> fmap length (Right "stuff") = length "stuff"
> fmap length (Left 5)          = length 5 ???? What does that even means ?
>
> Since the type contained by Left and Right are different, you can't in
> general apply the same function to both possibilities. Now the right type
> is the last one to occur in "Either left right" so it's the one that's
> abstracted over in the Functor instance, there is no Functor instance for
> Either, the instance is for "Either left" with the left type fixed, fmap
> don't touch the left type, so it can't do anything to a "Left x" value.
>
> Semantically, "Either left right" can be seen as a container of one or
> zero "right" that contains out-of-band information of type "left" when it's
> empty. So when you fmap over it, you only touch the content, not the
> out-of-band information.
>
> --
> Jeda?
>
> _______________________________________________
> 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/20150416/98bab247/attachment-0001.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

Greg Graham
On Thu, Apr 16, 2015 at 3:42 AM emacstheviking <objitsu at gmail.com> wrote:

> "Semantically, "Either left right" can be seen as a container of one or
> zero "right" that contains out-of-band information of type "left" when it's
> empty. So when you fmap over it, you only touch the content, not the
> out-of-band information."
>
> Yup. Whatever he said. ;)  ...that's bordering on monadic comparison
> tutorials if you ask me... LOL. A lot of people may not even know what "in
> band" and "out-of-band" even means. I grew up with X25, RS232, HDLC etc so
> that's ok by me. You have to be very very careful about how you explain
> things to  people. One of my favourite videos on YouTube is some guy asking
> Richard Feynman how magnets work...well, he did ask!
> https://www.youtube.com/watch?v=wMFPe-DwULM
>

I am just learning Haskell, having just finished working through _Learn You
a Haskell_, so I am no expert. However, the learning curve I am experience
reminds me of when I went from Applesoft BASIC to Pascal, and when I went
from C to C++. There was a lot of new terminology to learn such as
single-entry/single exit, procedures, formal and actual parameters,
encapsulation, polymorphism, early and late binding, and overloading.There
were plenty of nay-sayers that claimed that these paradigms introduced
unnecessary difficulties, but today you have to search far and wide for
someone who advocates GOTOs, and although O-O is not universally loved, it
is easily the dominant paradigm.

So, although I am still finding Haskell to be awkward for me, I am able to
wrestle with it until it clicks into place, and then it's really beautiful.
I feel like the benefits of the language will outweigh the effort it is
taking me to learn it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150416/12cee917/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

emacstheviking
Yeah. Learning Lisp did it for me....Haskell is super-strength Kiil-Aid
baby! Drink it and be changed forever.


On 16 April 2015 at 22:22, Greg Graham <greg at grahamtx.net> wrote:

> On Thu, Apr 16, 2015 at 3:42 AM emacstheviking <objitsu at gmail.com> wrote:
>
>> "Semantically, "Either left right" can be seen as a container of one or
>> zero "right" that contains out-of-band information of type "left" when it's
>> empty. So when you fmap over it, you only touch the content, not the
>> out-of-band information."
>>
>> Yup. Whatever he said. ;)  ...that's bordering on monadic comparison
>> tutorials if you ask me... LOL. A lot of people may not even know what "in
>> band" and "out-of-band" even means. I grew up with X25, RS232, HDLC etc so
>> that's ok by me. You have to be very very careful about how you explain
>> things to  people. One of my favourite videos on YouTube is some guy asking
>> Richard Feynman how magnets work...well, he did ask!
>> https://www.youtube.com/watch?v=wMFPe-DwULM
>>
>
> I am just learning Haskell, having just finished working through _Learn
> You a Haskell_, so I am no expert. However, the learning curve I am
> experience reminds me of when I went from Applesoft BASIC to Pascal, and
> when I went from C to C++. There was a lot of new terminology to learn such
> as single-entry/single exit, procedures, formal and actual parameters,
> encapsulation, polymorphism, early and late binding, and overloading.There
> were plenty of nay-sayers that claimed that these paradigms introduced
> unnecessary difficulties, but today you have to search far and wide for
> someone who advocates GOTOs, and although O-O is not universally loved, it
> is easily the dominant paradigm.
>
> So, although I am still finding Haskell to be awkward for me, I am able to
> wrestle with it until it clicks into place, and then it's really beautiful.
> I feel like the benefits of the language will outweigh the effort it is
> taking me to learn it.
>
> _______________________________________________
> 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/20150417/eb143a77/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Left vs Right

emacstheviking
Kool-Aid! It affects ones spelling too! LMAO

On 17 April 2015 at 08:10, emacstheviking <objitsu at gmail.com> wrote:

> Yeah. Learning Lisp did it for me....Haskell is super-strength Kiil-Aid
> baby! Drink it and be changed forever.
>
>
> On 16 April 2015 at 22:22, Greg Graham <greg at grahamtx.net> wrote:
>
>> On Thu, Apr 16, 2015 at 3:42 AM emacstheviking <objitsu at gmail.com> wrote:
>>
>>> "Semantically, "Either left right" can be seen as a container of one or
>>> zero "right" that contains out-of-band information of type "left" when it's
>>> empty. So when you fmap over it, you only touch the content, not the
>>> out-of-band information."
>>>
>>> Yup. Whatever he said. ;)  ...that's bordering on monadic comparison
>>> tutorials if you ask me... LOL. A lot of people may not even know what "in
>>> band" and "out-of-band" even means. I grew up with X25, RS232, HDLC etc so
>>> that's ok by me. You have to be very very careful about how you explain
>>> things to  people. One of my favourite videos on YouTube is some guy asking
>>> Richard Feynman how magnets work...well, he did ask!
>>> https://www.youtube.com/watch?v=wMFPe-DwULM
>>>
>>
>> I am just learning Haskell, having just finished working through _Learn
>> You a Haskell_, so I am no expert. However, the learning curve I am
>> experience reminds me of when I went from Applesoft BASIC to Pascal, and
>> when I went from C to C++. There was a lot of new terminology to learn such
>> as single-entry/single exit, procedures, formal and actual parameters,
>> encapsulation, polymorphism, early and late binding, and overloading.There
>> were plenty of nay-sayers that claimed that these paradigms introduced
>> unnecessary difficulties, but today you have to search far and wide for
>> someone who advocates GOTOs, and although O-O is not universally loved, it
>> is easily the dominant paradigm.
>>
>> So, although I am still finding Haskell to be awkward for me, I am able
>> to wrestle with it until it clicks into place, and then it's really
>> beautiful. I feel like the benefits of the language will outweigh the
>> effort it is taking me to learn it.
>>
>> _______________________________________________
>> 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/20150417/9fb4b470/attachment.html>