![](/img/trans.png)
[英]“Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String” In Haskell
[英]How to do when load error Couldn't match expected type `IO (Maybe String)'
module REPL(REPL(..), repl) where
import qualified Control.Exception as E
import System.Console.Readline(readline, addHistory)
data REPL s = REPL {
repl_init :: IO (String, s), -- prompt and initial state
repl_eval :: s -> String -> IO (Bool, s), -- quit flag and new state
repl_exit :: s -> IO ()
}
repl :: REPL s -> IO ()
repl p = do
(prompt, state) <- repl_init p
let loop s = (do
mline <- readline prompt
case mline of
Nothing -> loop s
Just line -> do
(quit, s') <- repl_eval p s line
if quit then
repl_exit p s'
else do
addHistory line
loop s'
) E.catch undefined (\(e :: E.SomeException) -> putStrLn "Handled exception!"
)
loop state
輸出:
REPL.hs:21:5:
Couldn't match expected type `IO (Maybe String)'
against inferred type `t -> Maybe String'
In a stmt of a 'do' expression: mline <- readline prompt
In the expression:
(do { mline <- readline prompt;
case mline of {
Nothing -> loop s
Just line
-> do { (quit, s') <- repl_eval p s line;
.... } } })
E.catch
undefined
(\ (e :: E.SomeException) -> putStrLn "Handled exception!")
In the definition of `loop':
loop s = (do { mline <- readline prompt;
case mline of {
Nothing -> loop s
Just line -> do { ... } } })
E.catch
undefined
(\ (e :: E.SomeException) -> putStrLn "Handled exception!")
自上一個問題以來,你已經刪除了反引號,`,圍繞你的E.catch
語句。 我對這個問題的回答沒有使用反引號,因為我沒有調用catch
綴。 你可以使用catch post-fix而不需要反引號:
let loop s = E.catch (do ...) (\(e :: E.SomeException) -> ...)
或者你可以使用catch infix和反引號:
let loop s = (do ...) `E.catch` (\(e :: E.SomeException) -> ...)
另外, undefined
是我只是為了GHCi REPL上的一個例子而強制異常 - 它取代了代碼中的(do ..)
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.