gpt4 book ai didi

haskell - 循环文件集?

转载 作者:行者123 更新时间:2023-12-02 17:56:40 24 4
gpt4 key购买 nike

在 Haskell 中,我尝试将文件列表处理为函数(类似于“for f in file1 file2 file3 file5 do ... in Bash”):

import System.Directory

returnDirectoryContents :: IO [FilePath]
returnDirectoryContents = do
let path = "."
filter (isSuffixOf ".txt") <$> getDirectoryContents path

countNumberOfWords :: FilePath -> IO Int
countNumberOfWords path = do ...

countNumberOfWords "file1.txt" 工作正常。但是,我无法将该函数映射到 FilePath 列表:

fmap countNumberOfWords returnDirectoryContents
• No instance for (Show (IO Int)) arising from a use of ‘print’

将文件列表处理到函数的正确方法是什么?

最佳答案

你想要map的monadic兄弟,mapM

关于haskell - 循环文件集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75569939/

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