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

Gregory Crosswhite gcross at phys.washington.edu
Mon Nov 16 14:45:52 EST 2009


In my own opinion, the reason why we use the concept of a monoid or a  
monad is in order to build libraries around the concepts.

For example, the "do" construct could have been designed just for  
doing IO, but because it works for *any* monad you can also use the  
same syntax sugar to conveniently work with a calculation

	*) that manipulates a mutable state (State)
	*) that has an environment (Reader)
	*) that writes out a log (Writer)

Likewise, the nice thing about monoid is that it lets us generalize  
libraries.  So for example, the Writer monad could have just been  
designed to work by concatenating strings or lists since this is what  
one might typically think of for a "log".  But because it is designed  
to work with an arbitrary *monoid*, you could use it to keep track of  
a running total as well, since numbers under addition is also a monoid.

So the way I figure it, the important thing is to understand just  
enough of these patterns (monads, monoids) that you can recognize them  
when they come up in your own work so that you can be aware of what  
pre-existing libraries and/or syntax sugar you can leverage to work  
with them.

Cheers,
Greg

On Nov 13, 2009, at 8:14 AM, Magicloud Magiclouds 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



More information about the Haskell-Cafe mailing list