I don't understand this type error:
Couldn't match expected type `[t0]' with actual type `IO ()'
In the return type of a call of `printv'
In a stmt of a 'do' expression: px <- printv x
In the expression:
do { px <- printv x;
sep <- print ", ";
rest <- prints xs;
return (px : sep : rest) }
From:
data Value = IntValue Int
| TruthValue Bool
deriving (Eq, Show)
printv :: Value -> IO()
printv (IntValue i) = print i
printv (TruthValue b) = print ("boolean" ++ show b)
prints :: [Value] -> [IO()]
prints [] = []
prints (x:xs) = do px <- printv x
sep <- print ", "
rest <- prints xs
return (px:sep:rest)
It looks to me like every element (px
) is converted into an IO()
action, and then that is added to a list of the same things, thus producing an [IO()]
list.
What am I missing here? Converting it to a list of strings, by removing the print's, works fine.
Take a closer look at the type of your function:
prints :: [Value] -> [IO()]
But if we now take a look at prints [] = []
, this can't match, because the type of that one is
prints :: [t] -> [a]
Therefore, you missed using prints [] = return []
, to make it work.