<p dir="ltr"><br>
Le 23 oct. 2012 09:54, &quot;Alfredo Di Napoli&quot; &lt;<a href="mailto:alfredo.dinapoli@gmail.com">alfredo.dinapoli@gmail.com</a>&gt; a écrit :<br>
&gt;<br>
&gt; What this code does is straighforward. I was struck from the following sentences in LYAH: <br>
&gt;<br>
&gt;&gt; Notice that we didn&#39;t have to provide the function that takes a value and returns a monoid value. <br>
&gt;&gt; We receive that function as a parameter to foldMap and all we have to decide is where to apply <br>
&gt;&gt; that function and how to join up the resulting monoids from it.<br>
&gt;<br>
&gt;<br>
&gt; This is obvious, so in case of (+) f = Sum, so f 3 = Sum 3 which is a Monoid.<br>
&gt; 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>
&gt; In other term, I&#39;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&#39;t know it has to pass anything ! As the doc said, this is a parameter to foldmap : so you would use &quot;treeSum = foldmap Sum&quot; for example. And the binary function associed would be determined by the monoid instance. Here that would be (+) because that&#39;s what mappend is for Sum (or rather for Sum Int).</p>

<p dir="ltr">-- <br>
Jedaï<br>
</p>