Reputation: 64
I have a problem with a Haskell script. I'm trying to learn Haskell through doing problems I find online. The input I get is: Int -> Number of test cases S1 -> String 1 for each test case S2 -> String 2 for each test case
Each S1 and S2 is a space delimited string of numbers. I convert them to a list of Ints with the function strToIntList
. I then want to process the two lists and return an integer value based on the results. I get the following error: Couldn't match type 'IO Integer' with 'Int'
on line 24 and I stared for ages but just can't figure out why (full error at end of post).
If someone could explain why I'm going wrong, I would be very grateful.
This is my script:
import Data.List
import Data.List.Split
main = do
cases <- readLn
doLoop cases
toInt x = read x :: Int
strToIntList s = [read x :: Int | x <- (splitOn " " s)]
minOfTwo :: Int
minOfTwo = do
sa <- getLine
sb <- getLine
return $ minimum [1..50]
-- return $ minimum $ strToIntList sa
doLoop 0 = return ()
doLoop loopIndex = do
q <- getLine
let c = minOfTwo
print(c)
doLoop (loopIndex-1)
This is the full error I'm getting:
Couldn't match type `IO Integer' with `Int'
Expected type: IO String -> (String -> IO Integer) -> Int
Actual type: IO String -> (String -> IO Integer) -> IO Integer
In a stmt of a 'do' block: sa <- getLine
In the expression:
do { sa <- getLine;
sb <- getLine;
return $ minimum [1 .. 50] }
In an equation for `minOfTwo':
minOfTwo
= do { sa <- getLine;
sb <- getLine;
return $ minimum [1 .. 50] }
Upvotes: 1
Views: 1990
Reputation: 62818
The getLine
function is in the IO
monad, and therefore any function that calls getLine
must also be in the IO
monad. Change your type signature for minOfTwo
from Int
to IO Int
, and that particular problem will go away.
(You'll also need to change let c = minOfTwo
into c <- minOfTwo
.)
There may be other errors, but this is the one causing your error message.
Upvotes: 3