Walking a directory in Haskell I wrote a function that recursively walks a given directory.

module WalkDir (walkDir) where

import System.Directory (doesDirectoryExist, getDirectoryContents)
import System.FilePath ((&lt;/&gt;))

walkDir :: FilePath -&gt; IO [FilePath]
walkDir r = contents &gt;&gt;= fmap concat . traverse helper
    where contents = fmap (r &lt;/&gt;) . filter ((&amp;&amp;) . (/=) "." &lt;*&gt; (/=) "..") &lt;$&gt; getDirectoryContents r
          helper x = do e &lt;- doesDirectoryExist x
                        if e then walkDir x else return [x]


However, I have several concerns with this function. For starters, it is slow and it does not print out results until they have all been collected (not as lazy as it should be). My best guess is that this is because of the constant concatenations. 

Additionally, the use of do-notation in helper seems clunky. This is where I'd love it if if were just a function because I could just use &gt;&gt;= with no do required. Alternatively if there were a GHC extension equivalent to LambdaCase for if statements that would also work.
As you noticed, this function is slow because it collects all the results before it starts printing them. To circumvent that problem, you need to interleave the collection of information and its printing.

A good way of doing that whilst keeping a compositional approach to solving the problem is to introduce a datatype reifying the structure of walkDir's call graph. Instead of sequencing all IO actions and getting a list of FilePaths back, you'd build a tree describing the computation (RTree for Rose Tree and T for Transformer as it takes an m):

data RTreeT m a = Node a [m (RTreeT m a)]


You can now write walkDir' describing your strategy to explore the directories on your filesystem: return the files present in the current directory immediately and then explore the subdirectories one after the other.

walkDir' :: FilePath -&gt; IO (RTreeT IO [FilePath])
walkDir' r = do
  contents      &lt;- fmap (r &lt;/&gt;) . exceptLocal &lt;$&gt; getDirectoryContents r
  (files, dirs) &lt;- filesAndDirs contents
  return $ Node files $ fmap walkDir' dirs


where filesAndDirs partitions a list of FilePath depending on whether they are files or directories (using tagDirectories to perform that test).

  tagDirectories :: [FilePath] -&gt; IO [(FilePath, Bool)]
  tagDirectories = mapM (\ x -&gt; (x,) &lt;$&gt; doesDirectoryExist x)

  filesAndDirs :: [FilePath] -&gt; IO ([FilePath], [FilePath])
  filesAndDirs c = bimap (fmap fst) . partition (not . snd) &lt;$&gt; tagDirectories c
    where bimap f (a, b) = (f a, f b)


and exceptLocal is the filter you had in your original code snippet:

  exceptLocal :: [FilePath] -&gt; [FilePath]
  exceptLocal = filter ((&amp;&amp;) . (/=) "." &lt;*&gt; (/=) "..")


You now have an RTreeT IO [FilePath] and you can described a strategy to print it which will interleave printing some of the content and running some of the remaining IO actions:

printRTreeT :: Show a =&gt; RTreeT IO a -&gt; IO ()
printRTreeT (Node a mts) = print a &gt;&gt; mapM_ (printRTreeT =&lt;&lt;) mts


Of course, this is a rather crude printing function (e.g. you will notice quite a few empty lists if you have empty subdirectories) but it gives you an idea of how to proceed from there on.

If this is still slow, you may want to play the same sort of trick on filesAndDirs: rather than sequencing all tests in one go, you could want to have a structure allowing you to only deal with one FilePath at a time.