[Haskell-cafe] Could someone teach me why we use Data.Monoid?

Rafael Gustavo da Cunha Pereira Pinto RafaelGCPP.Linux at gmail.com
Fri Nov 13 11:33:42 EST 2009


Disclaimer: I don't really know all about category theory, so some
definitions might not be absolutely correct.

Monoid is the category of all types that have a empty value and an append
operation.

The best example is a list.

instance Monoid [a] where
        mempty  = []
        mappend = (++)


Why do I need it? Well, you can think of a function where you need to
incrementally store data.

Storing them to a Monoid, you can start with a list and then change to a
Set, without changing the function itself, because it would be defined based
on the Monoid operations.

instance Ord a => Monoid (Set a) where
    mempty  = empty
    mappend = union
    mconcat = unions

Hope I have helped!

Regards,

Rafael



On Fri, Nov 13, 2009 at 14:14, Magicloud Magiclouds <
magicloud.magiclouds at gmail.com> wrote:

> Hi,
>  I have looked the concept of monoid and something related, but
> still, I do not know why we use it?
>
> --
> 竹密岂妨流水过
> 山高哪阻野云飞
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


-- 
Rafael Gustavo da Cunha Pereira Pinto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091113/7581ac7f/attachment.html


More information about the Haskell-Cafe mailing list