<p dir="ltr"><br>
Le 23 oct. 2012 09:54, "Alfredo Di Napoli" <<a href="mailto:alfredo.dinapoli@gmail.com">alfredo.dinapoli@gmail.com</a>> a écrit :<br>
><br>
> What this code does is straighforward. I was struck from the following sentences in LYAH: <br>
><br>
>> Notice that we didn't have to provide the function that takes a value and returns a monoid value. <br>
>> We receive that function as a parameter to foldMap and all we have to decide is where to apply <br>
>> that function and how to join up the resulting monoids from it.<br>
><br>
><br>
> This is obvious, so in case of (+) f = Sum, so f 3 = Sum 3 which is a Monoid.<br>
> What I was wondering about is how Haskell knows that it has to pass, for example, Sum in case of (+) and Product in case of (*)?<br>
> In other term, I'm missing the logical piece of the puzzle which maps the associative binary function passed to fold up to our foldMap declaration.</p>
<p dir="ltr">Haskell doesn't know it has to pass anything ! As the doc said, this is a parameter to foldmap : so you would use "treeSum = foldmap Sum" for example. And the binary function associed would be determined by the monoid instance. Here that would be (+) because that's what mappend is for Sum (or rather for Sum Int).</p>
<p dir="ltr">-- <br>
Jedaï<br>
</p>