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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:45:04+00:00 2026-05-16T23:45:04+00:00

I have to solve a following problem. there are many files let’s say 3

  • 0

I have to solve a following problem.

there are many files let’s say 3 for example, with the following content

file1

a1

a2

a3

a4

a5

a6

……

file2

b1

b2

b3

b4

b5

b6

……

file3

c1

c2

c3

c4

c5

c6

……

my program has to take filenames in parameter, read those files and print the following result

“a1 b1 c1”

“a2 b2 c2”

“a3 b3 c3”

“a4 b4 c4”

“a5 b5 c5”

“a6 b6 c6”

……

I’ve already wrote one version of the program, but it start by reading all the files, and that is very inneficient because those file can be 200Mb in size.

how to write a program to retrieve a line in each file and displays my matches before moving on to the following lines in the files. This certainly will avoid loading all the files and make good use of the garbage collector?

profile images

sorry i dont know how to insert images here, it always fails, but while profiling, memory usage looks like stairs from the top to the bottom

alt text

ok it’s works 1

thanks for reply

Answer

Thanks to FUZxxi his answer really help me, but there was a problem when files did not have the same number of line, to solve this problem, I’ve re-write his program this way

printLines :: [[String]] -> IO ()

printLines [] = return ()

printLines ss = do

    ss' <- printFirstLine ss

    if and $ map null ss' then putStrLn "finish" else printLines ss'





printFiles :: [FilePath] -> IO ()

printFiles paths = do

  files <- mapM readFile paths

  let fileLines = map lines files

  printLines fileLines



sliceFirstRow :: [[String]] -> ([String],[[String]])

sliceFirstRow list = unzip $ map getFirst list



printFirstLine :: [[String]] -> IO ([[String]])

printFirstLine ss = do

  let (fline,lline) = sliceFirstRow ss

  mapM_ putStrLn fline

  return lline    



getFirst :: [String] -> (String, [String])

getFirst [] = ("",[])

getFirst (x:xs) = (x,xs)

Thanks again

  • 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-16T23:45:04+00:00Added an answer on May 16, 2026 at 11:45 pm

    I would do something like this:

    printFiles :: [FilePath] -> IO ()
    printFiles paths= do
      files <- mapM readFile paths
      let fileLines = map lines files
      printLines fileLines
      where
      sliceFirstRow :: [[String]] -> ([String],[[String]])
      sliceFirstRow list = unzip $ map helper list where
        helper (x:xs) = (x,xs)
        helper []     = "" -- Your behaviour here
      printFirstLine :: [[String]] -> IO ([[String]])
      printFirstLine ss = do
        let (fline,lline) = sliceFirstRow ss
        mapM_ putStrLn fline
        return lline
      printLines :: [[String]] -> IO ()
      printLines [] = return ()
      printLines ss = do
        ss' <- printFirstLine ss
        printLines ss'
    

    Although not tested, it should be good for memory.

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

Sidebar

Related Questions

I have an idea for how to solve this problem, but I wanted to
I have a problem I'm trying to solve involving interfaceing a C++ program with
I have a weird date rounding problem that hopefully someone can solve. My client
I have a small math problem I am trying to solve Given a number
I'm trying to solve the 3n+1 problem and I have a for loop that
In trying to solve the ajax back button problem I have found the Really
I sometimes have difficulties with other people who wish to solve a problem when
I have a script that constantly segfaults - the problem that I can't solve
I'm trying to use STL to solve the following problem (I don't want to
I came up with an interesting way to solve the following problem and I

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.