小编Vic*_*asé的帖子

Haskell,我可以调用没有IO输出的函数与monads一起工作吗?

为什么我不能这样做?它禁止在这个问题中使用'do':/我如何在列表中调用单词并同时产生IO?谢谢..这是我的实际代码:/

main :: IO()
main = 
     putStr "Name of File: " >>
     getLine >>=
     \st ->
    openFile st ReadMode >>=
    \handle ->
        hGetContents handle >>=
        \y ->
        words y >>=
        \strings ->
            strings !! 1 >>=
            \string->
                 putStr string
Run Code Online (Sandbox Code Playgroud)

[编辑]解决方案:

main :: IO()
main = 
     putStr "Name of File: " >>
     getLine >>=
     \st ->
    openFile st ReadMode >>=
    \handle ->
        hGetContents handle >>=
        \y ->
        return (words y) >>=
        \strings ->
            return (strings !! 1) >>=
            \string-> …
Run Code Online (Sandbox Code Playgroud)

io monads haskell

4
推荐指数
2
解决办法
542
查看次数

标签 统计

haskell ×1

io ×1

monads ×1