[Haskell-beginners] Left vs Right

Chaddaï Fouché chaddai.fouche at gmail.com
Wed Apr 15 19:39:36 UTC 2015


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


More information about the Beginners mailing list