[Haskell-beginners] Folders and sub-folders

Mike Houghton mike_k_houghton at yahoo.co.uk
Fri Feb 26 10:42:07 UTC 2016


Thanks.


I sweated it  bit more and got



isOk FilePath -> Bool
isOk  = not . isPrefixOf "."  

folders :: FilePath -> IO [FilePath]
folders fp  = do
    all <- getDirectoryContents fp
    z' <- filterM doesDirectoryExist $ map (fp </>) (filter isOk all)
    x' <- mapM (\x -> folders x) z'
    return $ z' ++ (concat x') ::

which seems to work.


> On 25 Feb 2016, at 19:07, Imants Cekusins <imantc at gmail.com> wrote:
> 
> Hello Mike,
> 
> below code find all files recursively from a starting point. It works.
> 
> You'd need to tweak it to find folders instead.
> 
> 
> import System.Directory
> import Data.List
> 
> 
> findAllFiles::FilePath -> IO [FilePath]
> findAllFiles base0 = gd1 base0
>>> = \list1 -> concatMap' recurse3 list1
>     where gd1 d1 = filter f2 <$> (getDirectoryContents d1)
>           f2 "." = False
>           f2 ".." = False
>           f2 _ = True
>           recurse3 md3 = doesDirectoryExist md3full
>>> = \isDir3 ->
>                        if isDir3 then findAllFiles md3full
>                        else pure [md3full]
>                    where md3full = base0 ++ "/" ++ md3
> 
> 
> 
> concatMap':: (a -> IO [b]) -> [a] -> IO [b]
> concatMap' m0 list0 = sequence (m0 <$> list0)
>>> = \list2 -> pure $ concat list2
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



More information about the Beginners mailing list