<br><br><div class="gmail_quote">On Fri, Jun 10, 2011 at 4:13 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><div></div><div class="h5">On Friday 10 June 2011, 13:49:23, Dmitri O.Kondratiev wrote:<br>
&gt; On Thu, Jun 9, 2011 at 11:31 AM, Max Bolingbroke<br>
&gt; &lt;<a href="mailto:batterseapower@hotmail.com">batterseapower@hotmail.com</a><br>
&gt;<br>
&gt; &gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt; If you want plain text serialization, &quot;writeFile &quot;output.txt&quot; . show&quot;<br>
&gt; &gt; and &quot;fmap read (readFile &quot;output.txt&quot;)&quot; should suffice...<br>
&gt; &gt;<br>
&gt; &gt; Max<br>
&gt;<br>
&gt; This code works:<br>
&gt;<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;      line &lt;- readFile &quot;output.txt&quot;<br>
&gt;      let xss2 = read line :: [[Int]]<br>
&gt;      print xss2<br>
&gt;<br>
&gt; As soon as complete file is returned as a single line, using &#39;fmap&#39;<br>
&gt; does not make sense here:<br>
&gt;      line &lt;- readFile &quot;output.txt&quot;<br>
&gt;      let xss2 = fmap read line<br>
&gt;<br>
&gt;  When to use &#39;fmap&#39;?<br>
<br>
</div></div>    xss2 &lt;- fmap read (readFile &quot;output.txt&quot;)<br>
<br>
or<br>
<br>
    xss2 &lt;- read `fmap` readFile &quot;output.txt&quot;<br>
<br>
But it might be necessary to tell the compiler which type xss2 ought to<br>
have, so it knows which `read&#39; to invoke, if it can&#39;t infer that from later<br>
use.<br>
</blockquote></div><br><br>Two questions:<br>1) Why to use &#39;fmap&#39; at all if a complete file is read in a single line of text?<br><br>2) Trying to use &#39;fmap&#39; illustrates 1) producing an error (see below):<br>
main = do<br>     let xss = [[1,2,3],[4,5,6],[7,8],[9]]<br>     writeFile &quot;output.txt&quot; (show xss)<br>     xss2 &lt;- fmap read (readFile &quot;output.txt&quot;) :: [[Int]]<br>     print xss2<br><br>== Error:<br>
 Couldn&#39;t match expected type `[String]&#39;<br>             with actual type `IO String&#39;<br> In the return type of a call of `readFile&#39;<br> In the second argument of `fmap&#39;, namely `(readFile &quot;output.txt&quot;)&#39;<br>
 In a stmt of a &#39;do&#39; expression:<br>     xss2 &lt;- fmap read (readFile &quot;output.txt&quot;) :: [[Int]]<br clear="all"><br>