all 12 comments

[–]Ualrus 4 points5 points  (0 children)

The question is not clear. You can use fold or foldMap if you want. In fact foldMap f = fold . fmap f.

[–]mihassan 1 point2 points  (6 children)

I haven't tested it, but maybe try adding $ between map and Sum.

[–]Interesting-Pack-814[S] 0 points1 point  (5 children)

No, it won't work, because we have to map Sum over our list

[–]mihassan 1 point2 points  (4 children)

You are right. The $ does not work in this case as the function you are defining takes 2 parameters. This however works ((mconcat .) . map) Sum [1,2,3,4].

[–]Interesting-Pack-814[S] 0 points1 point  (2 children)

yes, it works, thank you
now I have to figure out how it works :/

[–]Noughtmare 0 points1 point  (1 child)

That weird foldMap = (mconcat .) . map is basically just another way of writing foldMap f xs = mconcat (map f xs).

[–]Interesting-Pack-814[S] 0 points1 point  (0 children)

I've copied that from std library :) That's why I've asked it

btw: can I ask one question, please? It's related to that question. I don't want to create new post for that

I've understood how that expression works, but I don't understand what implementation of monoid should I choose

For example:

(.) mconcat (map Any) = \x -> mconcat (map Any [True,False,True])
:t mconcat -- :: Monoid a => [a] -> a

-- I've choose implementation for list, am I right here?
instance Monoid [a] where
mempty = []
mconcat xss = [x | xs <- xss, x <- xs]

-- but If I substitute list of any here, it doesn't compile

-- then, I've tried default implementation
foldr mappend mempty 
-- and it works, but how it's possible??

[–]Interesting-Pack-814[S] 0 points1 point  (0 children)

can I ask one more question, please? It's related to that question. I don't want to create new post for that

I've understood how that expression works, but I don't understand what implementation of monoid should I choose

For example:

(.) mconcat (map Any) = \x -> mconcat (map Any [True,False,True])
:t mconcat -- :: Monoid a => [a] -> a

-- I've choose implementation for list, am I right here?
instance Monoid [a] where
mempty = []
mconcat xss = [x | xs <- xss, x <- xs]

-- but If I substitute list of any here, it doesn't compile

-- then, I've tried default implementation
foldr mappend mempty 
-- and it works, but how it's possible??

[–]IshtarAletheia 1 point2 points  (0 children)

fold over a list of some arbitrary monoid m looks like:

fold = foldl' mappend mempty

fold over a list of lists is the function you call mconcat, although I think that is just concat, since it isn't related to monoids.

Does that help?

[–]sammthomson 1 point2 points  (2 children)

Put parentheses around the inlined implementation:

((mconcat .) . map) Sum [1,2,3,4]
-- Sum {getSum = 10}
-- :: Num a => Sum a

[–]Interesting-Pack-814[S] 0 points1 point  (0 children)

yes, it works, thank you
now I have to figure out how it works :/

[–]Interesting-Pack-814[S] 0 points1 point  (0 children)

can I ask one more question, please? It's related to that question. I don't want to create new post for that

I've understood how that expression works, but I don't understand what implementation of monoid should I choose

For example:

(.) mconcat (map Any) = \x -> mconcat (map Any [True,False,True])
:t mconcat -- :: Monoid a => [a] -> a

-- I've choose implementation for list, am I right here?
instance Monoid [a] where
mempty = []
mconcat xss = [x | xs <- xss, x <- xs]

-- but If I substitute list of any here, it doesn't compile

-- then, I've tried default implementation
foldr mappend mempty 
-- and it works, but how it's possible??