[ACCEPTED]-Haskell IO: Couldn't match expected type `IO a0' with actual type-haskell

Accepted answer
Score: 10

username is a String, but promptUsername is an IO String. You need to do something 1 like:

username <- promptUsername
let entry = Entry username "somepassword"
persist entry
Score: 0

Here's another variant.

main = do
  action <- cmdParser
  case action of
    Add -> do username <- promptUsername
              let entry = Entry username "somepassword"
              persist entry

promptUsername :: IO String
promptUsername = do
  putStrLn "Username to add to the password manager:"

-- fake definitions to make things compile

persist :: Entry ->  IO ()
persist = print

cmdParser :: IO Add
cmdParser = fmap (const Add) getLine

data Add = Add deriving Show
data Entry = Entry String String deriving Show

The problem is 11 just that promptUsername is an action not a String. The action 10 'returns a String', so it has type IO String, but 9 it is itself nothing like a String. Since 8 Entry x y requires a String in the x position, something 7 in the shape of an action could no more fit there 6 than a number or boolean could. So in defining 5 your complex action, main, you must 'extract' the 4 string that will result from the simpler 3 action promptUsername in any case of execution, and give 2 the String as the first argument the entry. Then 1 you do the persist action on the resulting Entry

More Related questions