我想用Haskell做一個目錄結構的遞歸下降。我只想根據需要檢索子目錄和文件(懶洋洋地)。Haskell中目錄的流式遞歸下降
我寫了下面的代碼,但是當我運行它,跟蹤顯示所有目錄的第一個文件之前訪問:
module Main where
import Control.Monad (forM, forM_, liftM)
import Debug.Trace (trace)
import System.Directory (doesDirectoryExist, getDirectoryContents)
import System.Environment (getArgs)
import System.FilePath ((</>))
-- From Real World Haskell, p. 214
getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topPath = do
names <- getDirectoryContents topPath
let
properNames =
filter (`notElem` [".", ".."]) $
trace ("Processing " ++ topPath) names
paths <- forM properNames $ \name -> do
let path = topPath </> name
isDirectory <- doesDirectoryExist path
if isDirectory
then getRecursiveContents path
else return [path]
return (concat paths)
main :: IO()
main = do
[path] <- getArgs
files <- getRecursiveContents path
forM_ files $ \file -> putStrLn $ "Found file " ++ file
我怎樣才能交錯文件處理與血統?動作在main
的以下forM_
之前執行的問題?
在[「搜索文件系統所謂的「看穿越的另一種方式」的部分「](http://book.realworldhaskell.org/read/io-case-study-a-library-for-searching-the-filesystem.html)Real World Haskell的章節也提供了一種更靈活的方式來瀏覽文件使用摺疊和迭代器的系統。 –
我(很明顯)從RWH拿走了函數'getRecursiveContents'。我沒有看到後面的部分。我會看一看。謝謝。 – Ralph
您可能想查看http://hackage.haskell.org/package/FilePather – singpolyma