5

私はまだ Haskell、特に IO モナドを理解しています。

ディレクトリパスのリストがあります。たとえば、

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

そして、このリストを、次のように、これらの各パスの完全修飾コンテンツのリストにマップします (.およびなし..)。

["/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", ...]

次のようなある種の二重マップでこれを行うことができると思います。

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

しかし、これは機能しません。私が得ているエラーはこれです:

program.hs:27:56:
    タイプ `[]' を `IO' と一致させることができませんでした
    予想されるタイプ: IO Char
      実際のタイプ: FilePath
    式: (pd </>) 内容
    `mapM' の最初の引数では、つまり
      `(\ 内容 -> (pd ) 内容)'

program.hs:27:84:
    予期されるタイプ `[FilePath]' と一致しませんでした
                実際のタイプ `IO [FilePath]' で
    `mapM' の 2 番目の引数では、つまり
      `(getDirectoryContents pathDir)'
    式では:
      地図M
        (\内容 -> (pd </>) 内容)
        (getDirectoryContents pd)
4

1 に答える 1

3

可能な解決策(インポート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's under IO [..]
        -- ^ this returns IO [[FilePath]]

もっと単純化する方法があるかもしれません。

于 2015-04-24T07:55:01.050 に答える