Skip to content

Instantly share code, notes, and snippets.

@fujimura
Last active December 21, 2015 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujimura/6255790 to your computer and use it in GitHub Desktop.
Save fujimura/6255790 to your computer and use it in GitHub Desktop.
import Control.Applicative ((<$>))
import Control.Monad
import System.Directory (doesDirectoryExist, doesFileExist,
getCurrentDirectory, getDirectoryContents)
entries :: FilePath -> IO [FilePath]
entries path = do
contents <- getDirectoryContents path
files <- filterM doesFileExist' contents
directories <- filter isSubDirectory <$> filterM doesDirectoryExist' contents
subdirEntries <- mapM entries (map expandPath directories)
return $ files ++ concat subdirEntries
where
expandPath x = path ++ "/" ++ x
doesFileExist' = doesFileExist . expandPath
doesDirectoryExist' = doesDirectoryExist . expandPath
isSubDirectory :: FilePath -> Bool
isSubDirectory "." = False
isSubDirectory ".." = False
isSubDirectory _ = True
main :: IO ()
main = do
p <- getCurrentDirectory
contents <- entries p
forM_ contents putStrLn
@fujimura
Copy link
Author

$ ./entries

Main.hs
Hi.hs
Compiler.hs
Config.hs
Context.hs
Directory.hs
FilePath.hs
Flag.hs
Option.hs
Template.hs
Types.hs
Version.hs
$ tree                                                                                                                                                       ~/work/hi/src master
.
├── Distribution
│   ├── Hi
│   │   ├── Compiler.hs
│   │   ├── Config.hs
│   │   ├── Context.hs
│   │   ├── Directory.hs
│   │   ├── FilePath.hs
│   │   ├── Flag.hs
│   │   ├── Option.hs
│   │   ├── Template.hs
│   │   ├── Types.hs
│   │   └── Version.hs
│   └── Hi.hs
└── Main.hs

2 directories, 12 files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment