[Haskell-cafe] Data.List / Map: simple serialization?
Ketil Malde
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
> text?
Because it's not 'map', it's more generalized. So the argument ('read'
here) is applied to whatever is "inside" the second argument ('readFile
...'). Here
xss2 <- read `fmap` readFile "output.txt"
is equivalent to
xss2 <- return . read =<< readFile "output.txt"
or
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],[9]]
> 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]].
-k
--
If I haven't seen further, it is by standing in the footprints of giants
More information about the Haskell-Cafe
mailing list