Reputation:
I've finished writing a Haskell program but I have troubles with the I/O and Main parts of it. I want to read input of the following format:
10 3
1 4 8
The first and second numbers are Ints, and the numbers of the second line should be made into an integer list, [Int]. The length of the list is equal to the second number on the first line.
I have the following code to read one Int at a time, however, it can only get it to work for the first line of input.
getInt :: IO Int
getInt = do
l <- getLine
return (read l)
What am I doing wrong?
Upvotes: 0
Views: 224
Reputation: 52270
you should be able to get the data shown here by:
readData :: IO (Int, [Int])
readData = do
[nr, _] <- map read . words <$> getLine
nrs <- map read . words <$> getLine
return (nr,nrs)
this will read both lines and return the 10
as the first component and [1,4,8]
as the second component of a tuple if you read in your example
As the second number in the first line will just be the length
of the returned list I skipped it here.
of course you probably will want to adapt this to your needs.
Upvotes: 1