【发布时间】:2010-09-22 17:54:07
【问题描述】:
当我在 Haskell 中打开一个文件进行读取时,我发现关闭它后我无法使用该文件的内容。例如,这个程序将打印一个文件的内容:
main = do inFile <- openFile "foo" ReadMode
contents <- hGetContents inFile
putStr contents
hClose inFile
我预计将putStr 行与hClose 行互换不会产生任何效果,但是这个程序什么也不打印:
main = do inFile <- openFile "foo" ReadMode
contents <- hGetContents inFile
hClose inFile
putStr contents
为什么会这样?我猜这与惰性评估有关,但我认为这些表达式会被排序,所以不会有问题。你将如何实现像readFile 这样的函数?
【问题讨论】: