
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

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@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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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
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@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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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
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
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@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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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\").

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"
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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
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"
пише: 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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

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"
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
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"
пише: 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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

15 квіт. 2015 19:27 "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. :)
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ï

"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é
15 квіт. 2015 19:27 "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. :)
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Thu, Apr 16, 2015 at 3:42 AM 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
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.

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
On Thu, Apr 16, 2015 at 3:42 AM emacstheviking
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Kool-Aid! It affects ones spelling too! LMAO
On 17 April 2015 at 08:10, 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
wrote: On Thu, Apr 16, 2015 at 3:42 AM emacstheviking
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (9)
-
Alex Hammel
-
Bob Ippolito
-
Chaddaï Fouché
-
Chas Leichner
-
emacstheviking
-
Greg Graham
-
Kostiantyn Rybnikov
-
Mike Meyer
-
Shishir Srivastava