<div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div dir="ltr"><div><font face="arial, sans-serif"><span style="border-collapse: collapse;">This is the annoying part about Haskell . I can not understand composition .</span></font></div>

</div></blockquote><div><br>One of the ways of understanding composition (and many other functions in Haskell) is by trying to understand its type. Here it is shown by looking at the type in the interpreter GHCi.<br><br>
*Main&gt; :t (.)<br>
(.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c<br><br>It&#39;s a function that takes three arguments, the first two of which are functions, and the third is something else. Since (.) is a polymorphic function, we can tell everything about it (if we ignore certain other features of Haskell which are not immediately relevant). To give names to things, let&#39;s say composition is declared as follows:<br>

<br>(.) f g x = ...<br><br>We know that the type of x must be the same as the type of the argument to the function f. The result type of f is the same and the input type of g. The result type of g is the result type of (.). From these observations, we know that (.) applies g to x and f to the result of that. We can even write that definition down.<br>

<br>(.) f g x = f (g x)<br><br>But, in the case of (.), we don&#39;t need to look at the definition to understand how it works. We can get all the information from the type.<br><br>The next step in understanding (.) is seeing how it&#39;s used. If you want to compose two functions, you can use their types to figure out what the result is. You mentioned (.) (||), so let&#39;s look at that first. Then, we can use GHCi to verify our understanding.<br>

<br>The type of (||) is Bool -&gt; Bool -&gt; Bool or, equivalently, Bool -&gt; (Bool -&gt; Bool) (since -&gt; is right associative). If we apply (.) to (||), then we can substitute parts of the type of (||) into the type of (.) to figure out the type of (.) (||) (which is equivalent to ((||) .).<br>

<br>First, note the types of the two components:<br><br>(.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c<br>(||) :: Bool -&gt; (Bool -&gt; Bool)<br><br>Then, since (||) is plugged into the first argument of (.), we bind the right-hand sides below (from the type of (||)) to the left-hand sides (from (.)):<br>

<br>b = Bool<br>c = Bool -&gt; Bool<br><br>The resulting type is:<br><br>(.) (||) :: (a -&gt; Bool) -&gt; a -&gt; Bool -&gt; Bool<br><br>and GHCi agrees. If you are looking for something of this type, then you&#39;ve found it. Otherwise, you need to rethink your definition.<br>

<br>Regards,<br>Sean<br></div></div>