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