I have 5000 vectors which are held in 5000 files. I need to find their sum. Type DF2 is just a synonym for Vector Double
and made to be an instance of Num. So I read and parse all those files to list [IO DF2]
and fold it:
getFinal :: IO DF2
getFinal = foldl1' (liftA2 (+)) $ map getDF2 [1..(sdNumber runParameters)]
where getDF2 i = fmap parseDF2 $ readFile ("DF2/DF2_" ++ show i)
However I get an error:
DF2: DF2/DF2_1022: openFile: resource exhausted (Too many open files)
Google revealed this question to be very common:
However, I didn't get what is the problem with the lazy IO. If it is lazy, then why does it open files before they are needed? I didn't understand either how to adapt the elegant solution by Duncan Coutts to my case.