Is there a way to `read` lazily?

haskell

Solution

Daniels answer can be extended to parse the whole list at once using this function. Then you can directly access it as a list the way you want

lazyread :: Read a => [Char] -> [a]
lazyread xs = go (tail xs)
    where go xs = a : go (tail b)
        where (a,b) = head $ reads xs

Problem

I have probably just spend a day of computation time in vain :) The problem is that I (naively) wrote about 3.5GB of (compressed) `[(Text, HashMap Text Int)]` data to a file and at that point my program crashed. Of course there is no final `]` at the end of the data and the sheer size of it makes editing it by hand impossible. The data was formatted via `Prelude.show` and just at this point I realize that `Prelude.read` will need to the whole dataset into memory (impossible) before any data is returned. Now ... is there a way to recover the data without resorting to write a parser manually? Update 1 ``` main = do s <- getContents let hs = read s :: [(String, M.Map String Integer)] print $ head hs ``` This I tried ... but it just keeps consuming more memory until it gets killed by the OS.

Original source