Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7000251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:38:49+00:00 2026-05-27T20:38:49+00:00

I have 5000 vectors which are held in 5000 files. I need to find

  • 0

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:

  • Haskell lazy I/O and closing files
  • Processing (too) many XML files (with TagSoup)

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:38:50+00:00Added an answer on May 27, 2026 at 8:38 pm

    It’s not that it opens files before they’re needed; it’s that it doesn’t close them until you force the entire string. A simple way to work around this problem is to force the entire string immediately after reading it; since Vectors are strict, the simplest way to do this is to force the Vector to be evaluated after parsing it:

    getFinal :: IO DF2
    getFinal = foldl1' (liftA2 (+)) $ map getDF2 [1..(sdNumber runParameters)]
        where getDF2 i = readFile ("DF2/DF2_" ++ show i) >>= evaluate . parseDF2
    

    This uses Control.Exception.evaluate; you can think of evaluate as forcing its argument and then returning it. This only works if parseDF2 consumes the whole string, however.

    A more elegant solution would be to move away from lazy IO entirely, and use iteratees or something of the sort. But that’s probably not worth it for such a simple use-case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have more than 5000 image links and I need to find a way
I have about 5000 html files in a folder. I need to loop through
Lets say I have multiple commands like 5000 which updates some column and row
I have a large 100mb file which I would like to perform about 5000
I am scanning 5000 csv files into a database. Regrettably the files have '',
I have this variable std::vector <cv::KeyPoint> X[e]; with e as the number of files
I am trying to calculate the zscore for a vector of 5000 rows which
I have 5000 records, two fields of which contain Categories and SubCategories . There
If I have: #define MAXLINE 5000 What type is MAXLINE understood to be? Should
I have about 5000 markers I need to render on Google Map. I'm currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.