Adrien Varet
Adrien Varet

Reputation: 433

Haskell : Float to IO Float

I have a type problem with this function

type Store = [(String, Float)]
evalParser :: String -> Store -> Float -- Boring function who returns a Float

programme :: Store -> IO()
programme store = do
    putStr "@EvmInteractif:$> "
    instruction <- getLine
    putStrLn (show instruction)

    if (estCommande (instruction) == True) then
        do
            res <- evalParser instruction store
            putStrLn "TODO: execution de la commande"
            programme store

And at the line of res's affectation I have this error :

Couldn't match expected type IO Float' with actual typeFloat' In the return type of a call of `evalParser' In a stmt of a 'do' block: v <- evalParser expr store

Can someone tell me the rigth syntax please ? Thanks

Upvotes: 1

Views: 551

Answers (1)

Carcigenicate
Carcigenicate

Reputation: 45826

evalParser is pure, you don't need to use do notation to call it. To shimmy fix it, you could just wrap it in a return to stick it in the IO Monad:

res <- return $ evalParser instruction store

To avoid the wrapping/unwrapping altogether though, just don't treat it as an impure value from the start:

let res = evalParser instruction store

Upvotes: 2

Related Questions