में निर्देशिका की स्ट्रीमिंग रिकर्सिव वंश स्ट्रीमिंग मैं हास्केल का उपयोग करके निर्देशिका संरचना का एक पुनरावर्ती मूल करने की कोशिश कर रहा हूं। मैं केवल आवश्यकतानुसार बाल निर्देशिका और फ़ाइलों को पुनर्प्राप्त करना चाहता हूं (आलसी)।हास्केल
मैं निम्नलिखित कोड लिखा था, लेकिन जब मैं इसे चलाने, ट्रेस पता चलता है कि सभी निर्देशिकाओं पहली फ़ाइल से पहले दौरा कर रहे हैं:
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
मैं कैसे वंश के साथ फ़ाइल प्रोसेसिंग बिछा कर सकते हैं? क्या समस्या है files <- getRecursiveContents path
कार्रवाई main
में निम्नलिखित से पहले की जाती है?
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 ((</>))
import System.IO.Unsafe (unsafeInterleaveIO)
-- From Real World Haskell, p. 214
getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topPath = do
names <- unsafeInterleaveIO $ 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 unsafeInterleaveIO $ getRecursiveContents path
else return [path]
return (concat paths)
main :: IO()
main = do
[path] <- getArgs
files <- unsafeInterleaveIO $ getRecursiveContents path
forM_ files $ \file -> putStrLn $ "Found file " ++ file
वहाँ एक बेहतर तरीका है:
बाद में खंड [ "फाइल सिस्टम सर्च कर रहे हैं में" ट्रेवर्सल को देखने का एक और तरीका है "कहा जाता है "] (http://book.realworldhaskell.org/read/io-case-study-a-library-for-searching-the-filesystem.html) रीयल वर्ल्ड हास्केल का अध्याय फ़ाइल को नेविगेट करने का एक और अधिक सुविधाजनक तरीका भी प्रदान करता है प्रणाली जो एक गुना और एक पुनरावर्तक का उपयोग करता है। –
मैंने (स्पष्ट रूप से) आरडब्ल्यूएच से 'getRecursiveContents' समारोह लिया। मैंने बाद के खंड को नहीं देखा। मैं एक नजर मार लूगां। धन्यवाद। – Ralph
आप http://hackage.haskell.org/package/FilePather – singpolyma