Przesyłanie strumieniowe rekurencyjne zejście z katalogu w Haskell
Próbuję wykonać rekurencyjne zejście struktury katalogów za pomocą Haskell. Chciałbym tylko pobierać katalogi i pliki podrzędne w razie potrzeby (leniwie).
Napisałem następujący kod, ale gdy go uruchomię, śledzenie pokazuje, że wszystkie katalogi są odwiedzane przed pierwszym plikiem:
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
Jak mogę przeplatać przetwarzanie plików przy opuszczaniu? Czy problem polega na tym, żefiles <- getRecursiveContents path
akcja zostanie wykonana przed następującymi czynnościamiforM_
wmain
?