I've got this fairly simple program to print out each subdirectory of the working dir.
import System.Directory (listDirectory, doesDirectoryExist)
main = do
dirs <- getDirectories
mapM_ putStrLn dirs
-- Get a list of directories within the working directory
getDirectories :: IO [FilePath]
getDirectories = do
contents <- listDirectory "."
fIO doesDirectoryExist contents
-- Filter a list with an IO predicate.
-- (In this use case, queries file system for directory status.)
fIO :: (a -> IO Bool) -> [a] -> IO [a]
fIO f [] = return []
fIO f (x:xs) = do
matches <- f x
rest <- fIO f xs
return $ (if matches then [x] else []) ++ rest
I also attempted this implementation of the fIO
function, and I'm not sure if it's legitimately less clear or if it only appears that way to me because I'm not used to reading Haskell-style syntax yet.
fIO f (x:xs) = do
matches <- f x
fIO f xs >>= return . ((if matches then [x] else []) ++)
Critique Request
Being a Haskell newbie, I'm sure a professional would change some things about this implementation. I'm curious what sticks out as awkward, non-idiomatic, or opaque. I'm also curious which implementation of the two fIO
s (if either) you would select, and why.