How to match and merge FilePaths files?

I still understand Haskell, especially the IO monad.

I have a list of directory paths, for example,

["/some/path", "/another/path", "/yet/another/path", "/still/more"]

and I want to map this list to the list of fully qualified contents of each of these paths (without .and ..), for example:

["/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 suppose I can do this with some kind of dual card, for example:

pathItems <- mapM (\pd -> MapM (\contents -> (pd </>) contents) (getDirectoryContents pd) pathDirs

but it does not work. The error I am 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)
+4
source share
1 answer

Possible solution (import System.IO System.Directory System.FilePath Control.Applicative):

concat <$> mapM (\pd -> map (pd </>) <$> getDirectoryContents pd) pathDirs
                                   -- ^ this is to work under IO (..)
                    --  ^ this is to affect what under IO [..]
        -- ^ this returns IO [[FilePath]]

Perhaps there is a way to simplify it.

+2
source

Source: https://habr.com/ru/post/1584394/