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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:12:56+00:00 2026-05-27T18:12:56+00:00

I am writing a Haskell program where I want to write to an already

  • 0

I am writing a Haskell program where I want to write to an already existing file. The program needs to generate each string before appending it to the file. So rather than do the entire calculation first and then append to the file, I would like the program to append each line as it is calculated.

Here is the code I attempted:

-- line in my do-notation of interet
-- filename = valid filename
-- records = list of record data types
appendFile fileName (map recordToString records)

recordToString :: Record -> String
recordToString r = club r ++ "," ++  mapName r ++ "," ++ nearestTown r ++ "," ++ terrain r ++ "," ++ mapGrade r ++ "," ++ gridRefOfSWCorner r ++ "," ++ gridRefOfNECorner r ++ "," ++ expectedCompletionDate r ++ "," ++ sizeSqKm r ++ ",\n"

I am interested in solving this problem with lazy evaluation

  • 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-27T18:12:56+00:00Added an answer on May 27, 2026 at 6:12 pm

    As C. A. McCann said, this should already be lazy; however, appendFile will most likely open the file in block-buffered mode by default, which means that lines won’t be flushed as they are created; instead, data will be written to the file a few thousand bytes at a time. To solve this problem, just roll your own function:

    import System.IO
    
    appendFileLines :: FilePath -> String -> IO ()
    appendFileLines fileName text =
      withFile fileName AppendMode $ \h -> do
        hSetBuffering h LineBuffering
        hPutStr h text
    

    Then you can use appendFileLines instead of appendFile, and the file will be written to one record at a time.

    What you might be thinking of, in terms of performing IO operations with lazy results, is called “lazy IO”; it’s generally frowned upon, but it’s not required to achieve the effect you want here. (If you know how readFile or getContents operate, for instance, that’s lazy IO.)

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

Sidebar

Related Questions

I'm writing a Haskell program which generates an XML file. Apparently it is considered
I'm writing a program which needs a UI. The program is in haskell. For
I'm writing a haskell program with GUI. When I write the following piece of
I'm writing a program that reads from a list of files. The each file
I'm writing a lexer in haskell. Here's the code: lexer :: String -> [Token]
I've got a Haskell program that needs to execute a separate (third party) binary;
I am trying to learn haskell by writing a simple file copy util: main
This is my first program using Haskell. I'm writing it to put into practice
I'm writing a small program with IO actions in Haskell here is module StackQuestion
I'm writing an audio program in Haskell using Portaudio. I have a function that

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.