当给定由\n分隔的文本输入文件时,此程序产生我期望的输出:
import System.IO
main :: IO ()
main = do h <- openFile "test.txt" ReadMode
xs <- getlines h
sequence_ $ map putStrLn xs
getlines :: Handle -> IO [String]
getlines h = hGetContents h >>= return . lines
Run Code Online (Sandbox Code Playgroud)
用withFile代替openFile并稍微重新排列
import System.IO
main :: IO ()
main = do xs <- withFile "test.txt" ReadMode getlines
sequence_ $ map putStrLn xs
getlines :: Handle -> IO [String]
getlines h = hGetContents h >>= return . lines
Run Code Online (Sandbox Code Playgroud)
我设法完全没有输出.我很难过.
编辑:不再难过了:感谢一个人和所有人的深思熟虑和发人深省的答案.我在文档中做了一些阅读,并了解到withFile可以理解为支架 …
haskell ×1