Haskell程序:如何递归读取文件并搜索其内容?

时间:2021-05-04 22:33:27

I'm struggling to write a Haskell program which would cover at least these:

我正在努力编写一个至少包含以下内容的Haskell程序:

  1. Search through a given directory (recursively) for files containing a specified string

    在给定目录中(递归地)搜索包含指定字符串的文件

  2. Execution can be parameterized through command-line (typing --name SomeName will throw every file containing SomeName)

    执行可以通过命令行参数化(输入--name SomeName将抛出包含SomeName的每个文件)

  3. Can be run both Unix and Windows

    可以在Unix和Windows上运行

Considering above, how should reading files and searching functions should be written? Any advice would be much appreciated.

考虑到上述情况,应该如何编写读取文件和搜索功能?任何建议将不胜感激。

1 个解决方案

#1


See System.Directory.

To fetch the entries of a directory:

要获取目录的条目:

getDirectoryContents :: FilePath -> IO [FilePath] 

To check if an entry is a non-directory:

要检查条目是否是非目录:

doesFileExist :: FilePath -> IO Bool

To check if an entry is a directory:

要检查条目是否是目录:

doesDirectoryExist :: FilePath -> IO Bool

#1


See System.Directory.

To fetch the entries of a directory:

要获取目录的条目:

getDirectoryContents :: FilePath -> IO [FilePath] 

To check if an entry is a non-directory:

要检查条目是否是非目录:

doesFileExist :: FilePath -> IO Bool

To check if an entry is a directory:

要检查条目是否是目录:

doesDirectoryExist :: FilePath -> IO Bool