Thanks for the excellent explanation! : <br><br><div class="gmail_quote">On Fri, Jun 10, 2011 at 4:49 PM, Daniel Fischer <span dir="ltr">&lt;<a href="mailto:daniel.is.fischer@googlemail.com">daniel.is.fischer@googlemail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Friday 10 June 2011, 14:25:59, Dmitri O.Kondratiev wrote:<br>
&gt; Two questions:<br>
&gt; 1) Why to use &#39;fmap&#39; at all if a complete file is read in a single line<br>
&gt; of text?<br>
<br>
</div>Well, it&#39;s a matter of taste whether to write<br>
<br>
    foo &lt;- fmap read (readFile &quot;bar&quot;)<br>
    stuffWithFoo<br>
<br>
or<br>
<br>
    text &lt;- readFile &quot;bar&quot;<br>
    let foo = read text<br>
    stuffWithFoo<br>
<br>
The former saves one line of code (big deal).<br>
<div class="im"><br>
&gt;<br>
&gt; 2) Trying to use &#39;fmap&#39; illustrates 1) producing an error (see below):<br>
&gt; main = do<br>
&gt;      let xss = [[1,2,3],[4,5,6],[7,8],[9]]<br>
&gt;      writeFile &quot;output.txt&quot; (show xss)<br>
&gt;      xss2 &lt;- fmap read (readFile &quot;output.txt&quot;) :: [[Int]]<br>
<br>
</div>That type signature doesn&#39;t refer to xss2, but to the action to the right<br>
of the &quot;&lt;-&quot;, `fmap read (readFile &quot;output.txt&quot;)&#39;<br>
<br>
readFile &quot;output.txt&quot; :: IO String<br>
<br>
so<br>
<br>
fmap foo (readFile &quot;output.txt&quot;) :: IO bar<br>
<br>
supposing<br>
<br>
foo :: String -&gt; bar<br>
<br>
You want read at the type `String -&gt; [[Int]]&#39;, so the signature has to be<br>
<br>
    xss2 &lt;- fmap read (readFile &quot;output.txt&quot;) :: IO [[Int]]<br>
<div class="im"><br>
&gt;      print xss2<br>
&gt;<br>
&gt; == Error:<br>
&gt;  Couldn&#39;t match expected type `[String]&#39;<br>
&gt;              with actual type `IO String&#39;<br>
&gt;  In the return type of a call of `readFile&#39;<br>
&gt;  In the second argument of `fmap&#39;, namely `(readFile &quot;output.txt&quot;)&#39;<br>
&gt;  In a stmt of a &#39;do&#39; expression:<br>
&gt;      xss2 &lt;- fmap read (readFile &quot;output.txt&quot;) :: [[Int]]<br>
<br>
</div>Looking at the line<br>
<br>
    xss2 &lt;- fmap read someStuff :: [[Int]]<br>
<br>
the compiler sees that<br>
<br>
fmap read someStuff should have type [[Int]]<br>
<br>
Now, fmap :: Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b<br>
<br>
and [] is a Functor, so the fmap here is map, hence<br>
<br>
map read someStuff :: [[Int]]<br>
<br>
means<br>
<br>
someStuff :: [String]<br>
<br>
That&#39;s the expected type of (readFile &quot;output.txt&quot;), but the actual type is<br>
of course IO String, which is the reported error.<br>
</blockquote></div><br><br>