我知道您应该将要对结果执行的操作包装在 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.