Skip to content

Instantly share code, notes, and snippets.

@emctoo
Created June 16, 2012 05:39
Show Gist options
  • Save emctoo/2940095 to your computer and use it in GitHub Desktop.
Save emctoo/2940095 to your computer and use it in GitHub Desktop.
recursive list contents of a directory
-- from Real World Haskell, Chapter 9
module RecursiveContents (getRecursiveContents) where
import Control.Monad (forM)
import System.Directory (doesDirectoryExist, getDirectoryContents, getCurrentDirectory)
import System.FilePath ((</>))
getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topdir = do
names <- getDirectoryContents topdir
let properNames = filter (`notElem` [".", ".."]) names
paths <- forM properNames $ \name -> do
let path = topdir </> name
isDirectory <- doesDirectoryExist path
if isDirectory then getRecursiveContents path else return [path]
return (concat paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment