Reputation: 25
Here's my code:
mingle :: String -> String -> String
mingle (a:as) (b:bs) = ([a] ++ [b]) ++ mingle as bs
mingle [] [] = []
main = putStrLn "Enter 1st String:"
>> getLine
>>= \a -> read a >> putStrLn "Enter 2nd String:"
>> getLine
>>= \b -> read b >>= mingle a b
Error:
MingleStrings.hs:10:45:
Couldn't match type ‘[]’ with ‘IO’
Expected type: IO Char
Actual type: String
In the second argument of ‘(>>)’, namely ‘mingle a b’
In the expression: read b >> mingle a b
I was under the impression that read would be able to turn an IO type to a standard haskell type. None of the other posts about dealing with IO seemed to help.
Upvotes: 0
Views: 1481
Reputation: 48654
read
doesn't return IO
type as evidenced by it's type signature:
λ> :t read
read :: Read a => String -> a
What you want to do is this:
main = putStrLn "Enter 1st String:"
>> getLine
>>= \a -> putStrLn "Enter 2nd String:"
>> getLine
>>= \b -> return $ mingle a b
Since mingle
is a pure function, you have to use return
to inject IO
on top of that. Also note that your mingle
function doesn't handle all the cases. So you may want to fix that.
Upvotes: 3