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

sak*_*zai 14 directory file-io haskell

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

将当前目录更改为

 :cd c:/code/haskell
Run Code Online (Sandbox Code Playgroud)

然后编写一个函数,返回一组中的文件,例如

 [x | x <-getDirectoryContents ]
Run Code Online (Sandbox Code Playgroud)

编辑:

我写了一个像这样列出文件的函数(参考: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
Run Code Online (Sandbox Code Playgroud)

因此调用_dir"c:/ code/haskell"将列出所有文件+目录名称(非递归).我现在想要的是在谓词函数中调用它,例如:

[ x| x <- _dir  "c:/code/haskell" | x start with 'haskell_' ]  
Run Code Online (Sandbox Code Playgroud)

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

Ank*_*kur 20

看来你在寻找:

getDirectoryContents :: FilePath -> IO [FilePath]
Run Code Online (Sandbox Code Playgroud)

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

  • 只是在haskell中添加..它与你使用的其他编程语言不一样..涉及到不同的概念,当你学习haskell时,我建议你学习这些概念,然后试用代码,否则你会感到困惑.一本好的入门书可以是:http://learnyouahaskell.com/ (2认同)

stu*_*ith 8

以下内容如何:

import Data.List
import System.Directory

main = do all <- getDirectoryContents "/tmp/FOO"
          let filtered = filter (isPrefixOf "haskell") all
          print filtered
Run Code Online (Sandbox Code Playgroud)


csi*_*rra 7

编写一个返回当前目录中所有文件的函数所需要做的就是:

import System.Directory

filesInCurDir = getCurrentDirectory >>= getDirectoryContents
Run Code Online (Sandbox Code Playgroud)

>> =运算符是值传递的monad排序运算符.这里最好描述http://www.haskell.org/tutorial/monads.html.

如果你打算在ghci中使用它:

let filesInCurDir = getCurrentDirectory >>= getDirectoryContents
Run Code Online (Sandbox Code Playgroud)

你可以检查函数的类型,filesInCurDir :: IO [FilePath]从而保持"monadic nature".

因此,如果您想进一步过滤文件,您可以这样做:

let filteredFilesInCurDir = 
    getCurrentDirectory >>= 
    getDirectoryContents >>= 
    \files -> return [ x | x <- files, (length x) > 10 ]
Run Code Online (Sandbox Code Playgroud)

如果你想每次都通过过滤器:

let filterFilesInCurDir f = 
    getCurrentDirectory >>= 
    getDirectoryContents >>= 
    \files -> return [ x | x <- files, f x ]
Run Code Online (Sandbox Code Playgroud)

这与:

let filteredFilesInCurDir f = 
    getCurrentDirectory >>= 
    getDirectoryContents >>= 
    return . filter f
Run Code Online (Sandbox Code Playgroud)

你可以像以下一样使用它:

filterFilesInCurDir (\x -> (length x) > 2)
Run Code Online (Sandbox Code Playgroud)


phy*_*nfo 5

第一点:表达式[x | x <- lst]与表达式完全相同lst,因此如果lst是列表,则不需要使用列表推导.

第二:为了 [x | x <-getDirectoryContents ]工作,价值getDirectoryContents应该是一个清单.但事实并非如此!getDirectoryContents是一个IO值.

您可以通过以下方式使用此函数(在monadic表达式中):

do 
  files <- getDirectoryContents "."
  print files
Run Code Online (Sandbox Code Playgroud)

(或-内ghci中-使用:do; files <- getDirectoryContents "."; print files)

(files有类型 [FilePath]和while表达式有类型IO ())


ДМИ*_*КОВ 5

filemanip包为这个特定的任务提供了更通用和灵活的功能。

即列出给定目录中的常规文件

> :m + System.FilePath.Find
> :t find always (fileType ==? RegularFile)
find always (fileType ==? RegularFile) :: FilePath -> IO [FilePath]
Run Code Online (Sandbox Code Playgroud)

这个包是 Windows 兼容的(因为它依赖于unix-compat而不只是unix),并且还具有简洁的自我描述性 haddocs。