I would like to do something different but it would be too long so below is only example:
test x y = if x == "5" then x
else do putStrLn "bad value"; y
so if x == 5 it should return x, else it should print 'bad value' and return y – how can I do that in haskell ?
edit:
Why this code returns error: "couldn't match expected type bool with actual type IO bool" ?
canTest :: String -> IO Bool
canTest x = if x == "5" then return True
else do putStrLn "bad value"; return False
test x y = if canTest x then x
else y
Best Answer
You need to make both sides have the same type, namely
IO String
. For this you need to usereturn
to lift the values into the monad, i.e.Now
return x
has the typeIO String
, and so does thedo
block in the else branch.