I want to select the n
-th last line from a large text file (~10GB) in a Haskell program.
I found a way how to get the n
-th last from an internal string:
myLen = 7
n = 3 -- one-based from the end
myLines = lines myText
idx = myLen - n
theLine = head (drop idx myLines)
main :: IO ()
main = do
putStrLn theLine
The documentation about the readFile
function says it "reads the content lazily", so once readFile
got to the n
-th last line will it have stored all the lines before in memory (and then explodes because I don't have that much memory)?
So, is readFile
the right approach here? Plus how do I get the IO String
output from readFile
"in a lazy way" into a list of lines so that I can then select the n
-th last line?