Note that there are some explanatory texts on larger screens.

plurals
  1. POListing all the contents of a directory by breadth-first order results in low efficiency
    primarykey
    data
    text
    <p>I writed a Haskell module to list all the contents of a directory by breadth-first order. The below is the source code.</p> <pre><code>module DirElements (dirElem) where import System.Directory (getDirectoryContents, doesDirectoryExist) import System.FilePath ((&lt;/&gt;)) dirElem :: FilePath -&gt; IO [[FilePath]] dirElem dirPath = iterateM (not.null) (concatMapM getDirectoryContents') [dirPath] &gt;&gt;= return.tail getDirectoryContents' :: FilePath -&gt; IO [FilePath] getDirectoryContents' dirPath = do isDir &lt;- do doesDirectoryExist dirPath if isDir then dirContent else return [] where dirContent = do contents &lt;- getDirectoryContents dirPath return.(map (dirPath&lt;/&gt;)).tail.tail $ contents iterateM :: (Monad m) =&gt; (a -&gt; Bool) -&gt; (a -&gt; m a) -&gt; a -&gt; m [a] iterateM fb f x = do --Notice: Due to the the implementation of &gt;&gt;=, iterateM can't be writen like iterate which gives a infinite list and have type of iterateM :: (Monad m) =&gt; (a -&gt; Bool) -&gt; (a -&gt; m a) -&gt; a -&gt; m [a] if fb x then do tail &lt;- do {fx &lt;- f x; iterateM fb f fx} return (x:tail) else return [] concatMapM :: Monad m =&gt; (a -&gt; m[b]) -&gt; [a] -&gt; m[b] concatMapM f list = mapM f list &gt;&gt;= return.concat </code></pre> <p>It works correct but when performing on a large directory, it will "suspend" for a little while, and spring out all the results. </p> <p>After a research I find it is the same question with <code>sequence $ map return [1..]::[[Int]]</code> see <a href="https://stackoverflow.com/questions/14494648/why-the-haskell-sequence-function-cant-be-lazy-or-why-recursive-monadic-function">Why the Haskell sequence function can't be lazy or why recursive monadic functions can't be lazy</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload