相关疑难解决方法(0)

Haskell懒惰的I/O和关闭文件

我编写了一个小的Haskell程序来打印当前目录中所有文件的MD5校验和(递归搜索).基本上是Haskell版本的md5deep.一切都很好,花花公子,除非当前目录有大量的文件,在这种情况下,我收到如下错误:

<program>: <currentFile>: openBinaryFile: resource exhausted (Too many open files)
Run Code Online (Sandbox Code Playgroud)

似乎Haskell的懒惰导致它不会关闭文件,即使在相应的输出行已经完成之后也是如此.

相关代码如下.感兴趣的功能是getList.

import qualified Data.ByteString.Lazy as BS

main :: IO ()
main = putStr . unlines =<< getList "."

getList :: FilePath -> IO [String]
getList p =
    let getFileLine path = liftM (\c -> (hex $ hash $ BS.unpack c) ++ " " ++ path) (BS.readFile path)
    in mapM getFileLine =<< getRecursiveContents p

hex :: [Word8] -> String
hex = concatMap (\x -> printf "%0.2x" …
Run Code Online (Sandbox Code Playgroud)

haskell lazy-evaluation

20
推荐指数
3
解决办法
5010
查看次数

标签 统计

haskell ×1

lazy-evaluation ×1