Skip to content

Instantly share code, notes, and snippets.

@mbixby
Created April 11, 2012 12:29
Show Gist options
  • Save mbixby/2359042 to your computer and use it in GitHub Desktop.
Save mbixby/2359042 to your computer and use it in GitHub Desktop.
Translate directory tree to a tree
data Tree a = Leaf a | Node a [Tree a]
scanDir :: String -> IO (Tree String)
scanDir path = do
contents <- getDirectoryContents path `catch` const (return [])
let files = sort . filter (`notElem` [".", ".."]) $ contents
let pathsAndFiles = zip (repeat path) files
tree <- forM pathsAndFiles $ \(path, file) -> do
isDirectory <- doesDirectoryExist path
if isDirectory
then Node file (scanDir path++file)
else Leaf file
return tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment