开发者

couldn't match expected type [a0] with actual type IO ()

What is wrong in my code:

insertValue file x = 
    if x == "10" then "ok"
    else do put开发者_运维知识库StrLn "Error"; file


In an if..then..else expression, both branches have to have the same type.

One branch is:

"10" :: String

The other branch is:

do putStrLn "Error"; file :: IO ??

Since I'm not sure what you're trying to do (and the compiler isn't sure either), I don't know how to correct the code.


You need to use return :: a -> IO a to "lift" your strings into IO String:

insertValue file x = 
    if x == "10"
      then return "ok"
      else do putStrLn "Error"
              return file

But are you sure you don't want to call putStrLn "ok" (instead of return "ok") and return a Maybe value? Otherwise you are returning file or "ok" and your caller could never determine if there was an error when calling insertValue on a file named "ok".


"ok" is of type "String" while the "else" clause is of type "IO ()". In Haskell "if" is an expression, so they have to match.

Its difficult to help more without knowing what you are trying to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜