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
As C. A. McCann said, this should already be lazy; however,
appendFilewill 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:Then you can use
appendFileLinesinstead ofappendFile, 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
readFileorgetContentsoperate, for instance, that’s lazy IO.)