I am writing a haskell program and am having trouble with IO types. It seems something is wrong with my readLines function and the type it is supposed to return. I want it to return a list where each element is a line from the file that is opened with the openDict function.
Here is my code.
main :: IO ()
main = do
fileHandle <- openDict
readLines fileHandle
putStr "End of program\n"
readLines :: Handle -> [IO String]
readLines fileHandle
| isEOF <- hIsEOF fileHandle = []
| otherwise = [hGetLine fileHandle] ++ readLines fileHandle
openDict :: IO Handle
openDict = do
putStr "Enter file path: "
filePath <- getLine
openFile filePath ReadMode
Here is the error:
Couldn't match type `[]' with `IO'
Expected type: IO (IO String)
Actual type: [IO String]
In the return type of a call of `readLines'
In a stmt of a 'do' block: readLines fileHandle
In the expression:
do { fileHandle <- openDict;
readLines fileHandle;
putStr "End of program" }
So basically, how can I store these strings from IO in a list?
[IO String]doesn't make a lot of sense here. (This is not to say it doesn't make sense at all). Try working withIO [String].| isEOF <- hIsEOF fileHandledoesn't mean what you think it means. It does not actually check for EOF, it just bindshIsEOFwhich you are supposed to use later. This syntax makes sense for monads like Maybe, but not for IO. See paper.