-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentsofdir.hs
29 lines (24 loc) · 916 Bytes
/
contentsofdir.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{- this program prints specified extension files the contents of a directory-}
{- it takes function and path as input -}
{- example function (\p -> takeExtension p ==".hs") -}
{- Give a path name -}
import System.FilePath
import Control.Monad (forM)
import System.Directory (doesDirectoryExist, getDirectoryContents)
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)
simpleFind :: (FilePath -> Bool) -> FilePath -> IO ()
simpleFind p path = do
names <- getRecursiveContents path
let y= filter p names
mapM_ putStrLn y