This works:
c <- fmap lines (readFile "d:\\tmp\\h.txt")
let h = map (read :: String -> Int) c
while “superposition” of those two lines those not compile
fmap (read :: String -> Int) $ fmap lines (readFile "d:\\tmp\\h.txt")
it generates error:
interactive:1:36:
Couldn't match expected type `Char' with actual type `[Char]'
Expected type: String -> String
Actual type: String -> [String]
In the first argument of `fmap', namely `lines'
In the second argument of `($)', namely
`fmap lines (readFile "d:\\tmp\\h.txt")
Why it does not compile and how to do this in one line? What I want is achieve a simplicity of python
[int(i) for i in open("d:\\tmp\\h.txt")]
You left the
mapout of your “superposition” (composition):You can simplify that to
If you put an
import Control.Applicativeline at the top of your source file (or enter:m +Control.Applicativeif you’re using ghci interactively), you can use the<$>operator instead offmapto make it look cleaner. (They do exactly the same thing, they’re just spelled differently.)Finally, if you do need the type signature, you might find it looks clearer at the end of the line.