[Haskell-cafe] Data.List / Map: simple serialization?
ketil at malde.org
Fri Jun 10 14:42:19 CEST 2011
"Dmitri O.Kondratiev" <dokondr at gmail.com> writes:
>> xss2 <- read `fmap` readFile "output.txt"
> Two questions:
> 1) Why to use 'fmap' at all if a complete file is read in a single line of
Because it's not 'map', it's more generalized. So the argument ('read'
here) is applied to whatever is "inside" the second argument ('readFile
xss2 <- read `fmap` readFile "output.txt"
is equivalent to
xss2 <- return . read =<< readFile "output.txt"
tmp <- readFile "output.txt"
let xss2 = read tmp
> 2) Trying to use 'fmap' illustrates 1) producing an error (see below):
> main = do
> let xss = [[1,2,3],[4,5,6],[7,8],]
> writeFile "output.txt" (show xss)
> xss2 <- fmap read (readFile "output.txt") :: [[Int]]
> print xss2
fmap read (readFile "output.txt")
is of type IO [[Int]], not [[Int]].
If I haven't seen further, it is by standing in the footprints of giants
More information about the Haskell-Cafe