"Dmitri O.Kondratiev" <[email protected]> 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
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe