Haskell:
Writing a text file

How to:

Haskell’s standard Prelude provides elementary support for writing to files using the writeFile and appendFile functions from the System.IO module. Here’s a basic example of creating a new file (or overwriting an existing one) and then appending text to a file.

import System.IO

-- Writing to a file, overwriting if it exists
main :: IO ()
main = do
  writeFile "example.txt" "This is line one.\n"
  appendFile "example.txt" "This is line two.\n"

When you run this program, it creates (or clears) example.txt and writes “This is line one.” followed by “This is line two.” on the next line.

For more advanced file handling, Haskell programmers often turn to the text package for efficient string processing and the bytestring package for handling binary data. Here’s how to use the text package for file IO:

First, you need to add text to your project’s dependencies. Then, you can use it as follows:

import qualified Data.Text as T
import qualified Data.Text.IO as TIO

-- Writing to a file using the text package
main :: IO ()
main = do
  let content = T.pack "Using the text package for better performance.\n"
  TIO.writeFile "textExample.txt" content
  TIO.appendFile "textExample.txt" $ T.pack "Appending line two.\n"

In this snippet, T.pack converts a regular String to the Text type, which is more efficient. TIO.writeFile and TIO.appendFile are the text equivalents for writing and appending to files, respectively.

Running this code will result in a file named textExample.txt with two lines of text, demonstrating both creation and appending capabilities using the advanced text library for better performance and capability in handling Unicode text.