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

前端 未结 3 1855
梦毁少年i
梦毁少年i 2021-01-13 21:00

What is wrong in my code:

insertValue file x = 
    if x == \"10\" then \"ok\"
    else do putStrLn \"Error\"; file
3条回答
  •  一向
    一向 (楼主)
    2021-01-13 22:01

    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".

提交回复
热议问题