[Haskell-cafe] Data.List / Map: simple serialization?

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Jun 10 14:13:33 CEST 2011


On Friday 10 June 2011, 13:49:23, Dmitri O.Kondratiev wrote:
> On Thu, Jun 9, 2011 at 11:31 AM, Max Bolingbroke
> <batterseapower at hotmail.com
> 
> > wrote:
> > 
> > If you want plain text serialization, "writeFile "output.txt" . show"
> > and "fmap read (readFile "output.txt")" should suffice...
> > 
> > Max
> 
> This code works:
> 
> main = do
>      let xss = [[1,2,3],[4,5,6],[7,8],[9]]
>      writeFile "output.txt" (show xss)
>      line <- readFile "output.txt"
>      let xss2 = read line :: [[Int]]
>      print xss2
> 
> As soon as complete file is returned as a single line, using 'fmap' 
> does not make sense here:
>      line <- readFile "output.txt"
>      let xss2 = fmap read line
> 
>  When to use 'fmap'?

    xss2 <- fmap read (readFile "output.txt")

or

    xss2 <- read `fmap` readFile "output.txt"

But it might be necessary to tell the compiler which type xss2 ought to 
have, so it knows which `read' to invoke, if it can't infer that from later 
use.



More information about the Haskell-Cafe mailing list