Haskell 使用mapM\u/putStrLn打印映射函数的输出列表
我尝试使用putStrLn作为Haskell 使用mapM\u/putStrLn打印映射函数的输出列表,haskell,monads,Haskell,Monads,我尝试使用putStrLn作为 main = do let out = "hello\nworld\nbye\nworld\n" putStrLn $ map ("out: " ++) $ lines out 它抛出的错误是 Couldn't match type ‘[Char]’ with ‘Char’ 我引用了一些其他代码,并将最后一行更改为 mapM_ putStrLn $ map ("out: " ++) $ lines out 它解决了这个问题,但是在这种情况下,带下
main = do
let out = "hello\nworld\nbye\nworld\n"
putStrLn $ map ("out: " ++) $ lines out
它抛出的错误是
Couldn't match type ‘[Char]’ with ‘Char’
我引用了一些其他代码,并将最后一行更改为
mapM_ putStrLn $ map ("out: " ++) $ lines out
它解决了这个问题,但是在这种情况下,带下划线后缀的map monad是如何工作的?mapM_uu基于mapM函数,该函数具有
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
mapM_u的类型是
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
对于前者,它的行为类似于列表上的法线映射,但每个元素都有一个操作运行,并聚合结果。例如,如果你想读取多个文件,你可以使用内容