[Haskell-cafe] Could someone teach me why we use Data.Monoid?
David Leimbach
leimy2k at gmail.com
Fri Nov 13 12:30:23 EST 2009
On Fri, Nov 13, 2009 at 8:52 AM, Andrew Coppin
<andrewcoppin at btinternet.com>wrote:
> Stephen Tetley wrote:
>
>> 2009/11/13 Rafael Gustavo da Cunha Pereira Pinto <
>> RafaelGCPP.Linux at gmail.com>:
>>
>>
>>> Monoid is the category of all types that have a empty value and an append
>>> operation.
>>>
>>>
>>>
>>
>> Or more generally a neutral element and an associative operation:
>>
>> The multiplication monoid (1,*)
>>
>> 9*1*1*1 = 9
>>
>> 1 is neutral but you might be hard pressed to consider it _empty_.
>>
>>
>
> This is the thing. If we had a class specifically for containers, that
> could be useful. If we had a class specifically for algebras, that could be
> useful. But a class that represents "any possible thing that can technically
> be considered a monoid" seems so absurdly general as to be almost useless.
> If you don't know what an operator *does*, being able to abstract over it
> isn't especially helpful...
>
> ...in my humble opinion. (Which, obviously, nobody else will agree with.)
Well to your credit, many people wonder what it means for something to
implement the Monoid interface, or even why the Monoid interface is useful.
Programmers who don't know what Monoids are may not be aware of the varying
alternatives of Monoid implementations. They're curious things to have, but
are they useful? :-)
Sum vs Product spells out two ways to use Ints in a Monoid context. But we
could have done the same with the Functor implementation for lists via fmap
now couldn't we? Is anyone really using Sum and Product where Functors get
the job done?
Practically speaking, I can explain to coworkers the concept of a Functor a
lot easier than Monoids so I don't choose to use Monoids explicitly in my
code.
I do use MonadPlus though, especially with Maybe as it makes a nice
short-circuit syntax around alternatives for potential Nothing results. So
in a way I'm possibly shooting myself in the foot if I have to explain the
code to someone who was expecting a case or an if expression.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091113/ba78b4dd/attachment.html
More information about the Haskell-Cafe
mailing list