【发布时间】:2019-07-31 17:44:12
【问题描述】:
我知道您应该将要对结果执行的操作封装在 monad 中,而不是从 monad 中解包。
我找不到任何对白痴友好的示例。
例如,我想做这样的事情:
myFunction = do
c <- getChar
if (c == 'q')
then putStrLn "take action 1"
else putStrLn "take action 2"
但您不能直接将 char 文字与 IO Char 进行比较。
GHCi 版本为 8.4.4。
错误信息:
[1 of 2] Compiling Lib ( /Users/jamesstrieter/hask-tink/src/Lib.hs, interpreted ) /Users/jamesstrieter/hask-tink/src/Lib.hs:66:18: error: • Couldn't match expected type ‘IO char’ with actual type ‘Char’ • In the second argument of ‘(==)’, namely ‘'q'’ In the expression: x == 'q' In an equation for ‘what2do’: what2do x = x == 'q' • Relevant bindings include x :: IO char (bound at /Users/jamesstrieter/hask-tink/src/Lib.hs:66:9) what2do :: IO char -> Bool (bound at /Users/jamesstrieter/hask-tink/src/Lib.hs:66:1) | 66 | what2do x = x == 'q' | ^^^ Failed, no modules loaded.
【问题讨论】:
-
错误信息是什么?这段代码看起来不错。
-
你不能直接将 char 文字与 IO Char 进行比较 Apparently the code isn't doing that
-
向问题添加错误消息
-
您的错误消息似乎与您发布的代码完全不同。请在您的问题中提供代码来说明您的实际问题。
-
这很好。错误代码是针对一个相关但不同的问题,因此我将为此创建一个单独的问题。抱歉,我是新手。
标签: haskell monads io-monad do-notation