Reputation: 57
I am writing in Haskell and want to print a statement in a function and also call another function and I dont know why this isn't working. Could someone possibly tell me what I'm doing wrong or provide a more logical solution?
My error is:
Couldn't match expected type `[a0]' with actual type `IO ()'
In the return type of a call of `putStrLn'
In a stmt of a 'do' block: putStrLn "Missing closing bracket"
In the expression:
do { putStrLn "Missing closing bracket";
evaluate_input }
The code:
bracket_content [] 0 = []
bracket_content (first:rest) counter
| is_open_bracket first = first : bracket_content rest (counter + 1)
| is_close_bracket first = first : bracket_content rest (counter - 1)
| counter == 0 = []
| otherwise = first : bracket_content rest counter
bracket_content _ _ = do putStrLn "Missing closing bracket" --error here
evaluate_input
evaluate_input :: IO ()
evaluate_input = do
putStrLn "Enter Expression or 'q' to exit calculator: "
expression <- getLine
case expression of
a:as -> return a
unless (expression == ['q']) $ evaluate_expression expression
where evaluate_expression e = do
putStrLn . show $ calculate e
evaluate_input
Upvotes: 2
Views: 1675
Reputation: 21811
Your problem is that you are trying to make bracket_content
return two different types. The first two patterns return lists, while the last pattern returns an IO ()
. The compile error is indicating that GHC inferred (because you use cons (:
) on the recursive call) or read from your type signature the type of bracket_content
to be
bracket_content :: [a] -> Int -> [a]
but your last pattern returns an IO ()
. I think you might be trying to return a list of characters that you can later print: you are appending an open bracket to whatever the recursive call returns.
One potential solution is to make every pattern return an IO ()
:
bracket_content _ 0 = return ()
bracket_content (first:rest) counter
| is_open_bracket first = do putChar first
bracket_content rest (counter + 1)
| is_close_bracket first = do putChar first
bracket_content rest (counter - 1)
| otherwise = do putChar first
bracket_content rest counter
bracket_content _ _ = do putStrLn "Missing closing bracket"
evaluate_input
I'm not sure if this will accomplish what you want, but at least it compiles. The difference between your version and my version is that in my version, every pattern returns an IO (), giving your function the signature:
bracket_content :: [Char] -> Int -> IO ()
Also notice that I removed your guard
| counter == 0 = []
because I added it to the first pattern, which now prints nothing if the counter is 0, regardless of if the list is empty or not.
Upvotes: 1