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 7491965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:12:14+00:00 2026-05-29T16:12:14+00:00

Hello fellow coders. So i decided to rewrite some of my old scripts i

  • 0

Hello fellow coders.
So i decided to rewrite some of my old scripts i had lying around in haskell just because i need the practice and i like the language. So here i am trying to filter a huge file (around 1.7 GB) , cut the lines of no interest and write the remaining stuff in another file.

I thought that haskell’s lazy nature would be ideal for this but the code keeps running out of memory too soon. The previous versions (c# or Python) had a read line -> write line approach but i tried a different approach here. Should i just rewrite the code to mirror the previous version or am i missing something.

So this is the function in charge of the original file filtering:

getLines :: FilePath -> IO [[String]]
getLines path = do
    text<-readFile path
    let linii=lines text
    let tokens = map words linii
    let filtrate=[x|x<-tokens,length x>7,isTimeStamp (x!!0),isDiagFrame x]
    return filtrate

this one is in charge of writing one line at a time in the new file (altho i tried to use writeFile dirrectly and failed miserably 🙂 :

writeLines ::Handle->[[String]]->IO ()
writeLines handle linii = do
                    let linie=concat $ intersperse " " (head  linii)
                    hPutStrLn handle linie
                    if length linii > 0     then
                                    writeLines handle  (tail linii)
                                        else
                                    print "Writing complete..."

and these 2 are the main function and another one in charge of geting the handle and passing it around :

writeTheFile :: FilePath->FilePath->IO ()
writeTheFile inf outf = do
handle<-openFile outf WriteMode
linii<-getLines inf
writeLines handle linii
print "Write Complete"


main = do
arg<-getArgs
if length arg/=2    then
    print "Use like this : trace_pars [In_File] [Out_File] !"
                    else 
    writeTheFile (arg!!0) (arg!!1)

Any advice would be greatly appreciated…thanks in advance

  • 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-29T16:12:15+00:00Added an answer on May 29, 2026 at 4:12 pm

    The problem here is in this line:

                        if length linii > 0     then
    

    You are computing the length of your list of lines. This means that the whole list of lines has to be loaded for it to be counted. Which means that the whole file that you’re reading needs to be loaded into memory. Not good!

    The solution is to use if not . null $ linii then instead. The null function checks whether a list is empty (which only forces the first line of the list to be loaded), and not behaves like you’d expect.

    If you would like a more idiomatic version of writeLines (Note the use of FilePath instead of Handle):

    writeLines :: FilePath -> [[String]] -> IO ()
    writeLines filename = writeFile filename . unlines . map unwords
    

    This function is the same as:

    writeLines filename lines =
      writeFile filename mergedFile
      where
        mergedFile = unlines mergedLines
        mergedLines = map unwords lines
    

    unlines is the same as intercalate "\n", and unwords is the same as intercalate " ". intercalate x is the same as concat . intersperse x.

    I think that this should be enough information for you to understand what’s going on.

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

Sidebar

Related Questions

Hello fellow StackOverflowers! I have some information in a database that I need pulled
Hello fellow coders & codetts I was wondering, if I needed to create a
Hello , all my fellow coders! I've got a problem ohh chok! .. :P
Hello my fellow Stackoverflownians :), I just came across this thing called Zend. And
Hello fellow developers... just to make sure, I want to ask this question: How
Hello fellow Computer People! I could do this myself, but was just wondering if
Hello fellow earthlings! For some time I'm fantasizing possible solutions, but i'm out of
Hello fellow coder i am new to web programming and i need so major
Hello fellow programmers! I just started an additional programming project and swore to god
Hello Fellow Matlab Users, I was wondering if anyone could provide me with some

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.