How to insert a value into a monoid?

It just seemed to me that it was impossible to insert a single value into a monoid in Data.Monoid . I Data.Monad.return to use Data.Monad.return for this, but was hoping to find something like singleton for several types.

+7
source share
2 answers

The Monoid class Monoid not allow any injection, since the monoid is not a container of any type. Some containers are monoids, and then they will have their own means for injection. For a relatively general injection, you can use pure from Applicative or return from Monad (the first is more general).

+15
source

Adding to what others have said: Int forms a monoid (in several ways). How would you enter a value in Int ? Well, you won’t do it; a Int is just Int . Perhaps you can use zero or something else ...?

Now, if something is a container, it forms a monoid. But the monoid bit will not help you consider it as a container; you need to try something else for this. Many things that are not containers form monoids.

+6
source

All Articles