這工作:爲什麼這個函數組合是非法的
c <- fmap lines (readFile "d:\\tmp\\h.txt")
let h = map (read :: String -> Int) c
而這兩條線的 「疊加」 那些無法編譯
fmap (read :: String -> Int) $ fmap lines (readFile "d:\\tmp\\h.txt")
它產生錯誤:
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")
爲什麼它不能編譯以及如何在一行中完成此操作?我要的是實現的Python簡單
[int(i) for i in open("d:\\tmp\\h.txt")]
此外,我d將'read'移動到/ let綁定的位置,'fmap(map readInt。lines)$ readFile「foo」'是更可讀的IMO。 – 2012-04-20 15:52:34