I'm still figuring out Haskell, especially the IO monad.
I have a list of directory paths, e.g.,
["/some/path", "/another/path", "/yet/another/path", "/still/more"]
and I want to map this list into a list of fully-qualified contents of each of these paths (without .
and ..
), like this:
["/some/path/file.1", "/some/path/somedir", "/some/path/file.2", "/another/path/file.a", "/another/path/a/directory", "/another/path/file.b", "/still/more/file.alpha", ...]
I figure I could do this with some kind of double-map, like this:
pathItems <- mapM (\pd -> MapM (\contents -> (pd </>) contents) (getDirectoryContents pd) pathDirs
but this doesn't work. The error I'm getting is this:
program.hs:27:56: Couldn't match type `[]' with `IO' Expected type: IO Char Actual type: FilePath In the expression: (pd </>) contents In the first argument of `mapM', namely `(\ contents -> (pd ) contents)' program.hs:27:84: Couldn't match expected type `[FilePath]' with actual type `IO [FilePath]' In the second argument of `mapM', namely `(getDirectoryContents pathDir)' In the expression: mapM (\ contents -> (pd </>) contents) (getDirectoryContents pd)
concatMap
them together into a single list - right? – Random Dev(\ contents -> return $ (pd </>) contents)
instead. But this looks too complex, probablyfmap (map (pd </>)) (getDirectoryContents pd)
can replace the innermapM
. – chi