gpt4 book ai didi

haskell - 如何列出当前目录中的所有文件

转载 作者:行者123 更新时间:2023-12-03 21:03:52 25 4
gpt4 key购买 nike

我想要的是编写一个 Haskell 函数来返回当前目录的文件,例如

将当前目录更改为

 :cd c:/code/haskell

然后编写一个返回集合中文件的函数,例如
 [x | x <-getDirectoryContents ]

已编辑:

我写了一个这样的函数,它列出了文件
(引用: http://zvon.org/other/haskell/Outputdirectory/index.html)
import Directory 

main = _dir "/tmp/FOO"

_dir _path =do
setCurrentDirectory _path
_cd <- getCurrentDirectory
print _cd
_file <- getDirectoryContents _cd
print _file

因此调用 _dir "c:/code/haskell"将列出所有文件 + 目录名称(非递归)。我现在想要的是在谓词函数中调用它,例如:
[ x| x <- _dir  "c:/code/haskell" | x start with 'haskell_' ]  

所以我可以对文件名应用过滤器

最佳答案

看来您正在寻找:

getDirectoryContents :: FilePath -> IO [FilePath]

引用: http://www.haskell.org/ghc/docs/6.12.2/html/libraries/directory-1.0.1.1/System-Directory.html#1

关于haskell - 如何列出当前目录中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6649347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com