我正在将 getDirectoryContents 与 toText 导数与 fldmap 结合使用。
发生的错误是:
` IoString.hs:17:5:错误: • 无法匹配预期类型:FilePath -> IO () 实际类型:IO b0 • 在“do”块的stmt 中: 内容 <- getDirectoryContents "/home/lukas" In the expression: do contents <- getDirectoryContents "/home/lukas" contents >>= 映射打印到文本 在“main”的等式中: 主要的 = 执行内容 <- getDirectoryContents "/home/lukas" contents >>= 将打印映射到文本 | 17 | 17内容 <- getDirectoryContents "/home/lukas" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `` and more others below.
附件代码:
--{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Text (pack, Text)
import System.Directory (getDirectoryContents, getCurrentDirectory, makeAbsolute)
import GHC.Real (reduce)
parse xs = foldMap pack
toText :: [FilePath] -> Text
toText = foldMap pack
main :: FilePath -> IO ()
main = do
contents <- getDirectoryContents "/home/lukas"
contents >>= map print toText
尝试读取目录内容,期望打印。
基本的类似命令式的 for 循环:
import Data.Foldable (for_)
main :: IO ()
main = do
contents <- getDirectoryContents "/home/lukas"
for_ contents $ \f -> do
print f
这可以通过多种方式缩短,例如