[Haskell-beginners] How to use the Data.Map inbuild Monoid

Leonhard Applis Leonhard.Applis at protonmail.com
Mon Nov 18 22:22:01 UTC 2019


Hello Francesco,

> `Map k v` is already an instance of `Monoid` (when `v` is an instance of `Ord`), are you sure you need to write another one?

Thanks for your answer!
My "Values" is a Monoid, however it seems that the Graph cannot properly be seen correctly as a Monoid

using mconcat does not (properly) work, meaning that <> is not correctly applied to the someMonoidsso instead of

type G = Map Text someMonoid
mconcat [gs]

I have to write:

foldr (Map.unionWith (<>)) Map.empty [gs]

This passes the tests.
Same Problem with
g1 <> g2  --(Does not work properly)
and
Map.unionWith (<>) g1 g2 --(Does work)

I have declared someMonoid myself, do I need to declare something special about it?
I feel like my G is messing up with <> being about him, or about someMonoid

A broken down piece of code is:

importData.MapasMap

dataSum=SumIntderiving (Eq,Show)

instanceSemigroupSumwhere
(<>) (Sum a) (Sum b)=Sum(a + b) 
instanceMonoidSumwhere
    mempty =Sum0

typeG=Map.MapStringSum

And to verify my problem:
GHCI > v = Map.singleton "A" (Sum 1)
GHCI > u = Map.singleton "A" (Sum 1)
GHCI> c= v <> u
GHCI> Map.lookup "A" c
Just (Sum 1)

but I want
Just (Sum 2)

thanks
Leonhard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20191118/1a748dcd/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: publickey - Leonhard.Applis at protonmail.com - 0x807FDDF3.asc
Type: application/pgp-keys
Size: 1843 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20191118/1a748dcd/attachment-0001.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 477 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20191118/1a748dcd/attachment-0001.sig>


More information about the Beginners mailing list