我已经使用以下Haskell代码打印了目录列表:
import Control.Monad
import Control.Applicative
import System.Directory
main :: IO()
main = do
all <- listDirectory "x:/n"
mapM_ print all
但是现在我想使用System.Direcorty模块中的didFileExist函数过滤所有内容,并且无法理解如何正确使用它:
import Control.Monad
import Control.Applicative
import System.Directory
main :: IO()
main = do
all <- listDirectory "x:/n"
mapM_ print (filterM doesFileExist all)
上面的代码未编译并显示错误:
* No instance for (Foldable IO) arising from a use of `mapM_'
* In a stmt of a 'do' block:
mapM_ print (filterM doesFileExist all)
In the expression:
do all <- listDirectory "x:/n"
mapM_ print (filterM doesFileExist all)
In an equation for `main':
main
= do all <- listDirectory "x:/n"
mapM_ print (filterM doesFileExist all)
mapM_ print (filterM doesFileExist all)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我想我已经错过了一些基本的东西,所以请帮我找到一种方法来了解我的缺失。 谢谢
答案 0 :(得分:5)
使用另一个<-
:
main = do
all <- listDirectory "x:/n"
filtered <- filterM doesFileExist all
mapM_ print filtered