Cannot match expected type [a0] with actual type IO ()

What is wrong in my code:

insertValue file x = if x == "10" then "ok" else do putStrLn "Error"; file 
+4
source share
3 answers

In an if..then..else expression if..then..else both branches must be of the same type.

One branch:

 "10" :: String 

Another branch:

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

Since I'm not sure what you are trying to do (and the compiler is not sure either), I do not know how to fix the code.

+2
source

You need to use return :: a -> IO a to "raise" your strings in an 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 Maybe? Otherwise, you return file or "ok" and your caller cannot determine if there was an error calling insertValue in a file named "ok".

+2
source

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

Its hard to help anymore without knowing what you are trying to do.

+1
source

All Articles