user6917232
user6917232

Reputation:

error while getting input from a file

main function:

main = do
  x <- getLine
  lines <- replicateM x getLine
  print $ lines

the error I am getting:

Couldn't match type `[Char]' with `Int'
Expected type: Int
  Actual type: String
In the first argument of `replicateM', namely `x'
In a stmt of a 'do' block: lines <- replicateM x getLine

Need a way to solve this issue.

Upvotes: 0

Views: 57

Answers (1)

LycuiD
LycuiD

Reputation: 2575

replicateM goes like replicateM :: Monad m => Int -> m a -> m [a] where as getLine :: IO String
"x" needs to be Int but getLine returns an IO String, You can do read x :: Int:

main = do
  x <- getLine
  lines <- replicateM (read x :: Int) getLine
  print $ lines

or simply do:

main = do
  x <- readLn
  lines <- replicateM x getLine
  print $ lines

readLn probably gets x as IO Int

Upvotes: 4

Related Questions