Error in recursive function / trying to find all subdirectories of a given directory
<p>I'm trying to write a function that returns all subdirectories of a given directory (recursive).</p>

<p>What I have so far is:</p>

<pre><code>import System.Directory
import Control.Monad
import Data.List

getSubDirs :: FilePath -&gt; IO [FilePath]
getSubDirs dir =
        getDirectoryContents dir
        &gt;&gt;= filterM (\x -&gt; return $ x /= "." &amp;&amp; x /= "..")
        &gt;&gt;= mapM (makeAbsolute . (\x -&gt; dir ++ "/" ++ x))
        &gt;&gt;= filterM doesDirectoryExist
        &gt;&gt;= return . reverse

getDirTree :: [FilePath] -&gt; IO [FilePath]
getDirTree [] = return []
getDirTree l@(x:xs) = do
        a &lt;- getSubDirs x &gt;&gt;= getDirTree
        b &lt;- getDirTree xs
        return (nub $ l ++ a ++ b)
</code></pre>

<p>Which seems to work. But there is something wrong with the recursion - I would like to get rid of the <code>nub</code> in <code>getDirTree</code>. </p>