Haskell:
Reading a text file

How to:

Here’s how you get Haskell to read text files without breaking a sweat. Crack open your favorite editor, and let’s write some code.

import System.IO

main = do
    -- Open a file in read mode
    handle <- openFile "hello.txt" ReadMode
    -- Read the file's contents
    content <- hGetContents handle
    -- Print the file's contents
    putStrLn content
    -- Don't forget to close the file handle!
    hClose handle

Run this, and if you have “hello.txt” with “Hello, World!” inside, you get:

Hello, World!

Here’s a shorter, slicker way, doing the same with less fuss:

-- The 'readFile' does the open and read in one go
main = do
    content <- readFile "hello.txt"
    putStrLn content

Output’s still,

Hello, World!

Deep Dive

Long ago, programs were asocial creatures, mostly processing data they generated themselves. But complexity grew, and so did the need to pull in outside info, thus reading from files became a staple.

Haskell provides various ways to read files. We can do it the low-level way with openFile, hGetContents, and hClose or play it cool with readFile, which bundles everything neatly.

readFile is lazy – it reads contents as needed, which is memory efficient for large files but can lead to surprises if the file changes midway. The low-level approach gives more control, making it more predictable but also more verbose. For gargantuan texts, Haskell’s hGetLine or libraries like conduit and pipes help manage memory and processing more finely.

Haskell’s standard IO actions handle files using the underlying OS mechanisms. The libraries abstract these into more user-friendly operations but at the end of the day, they’re built atop Haskell’s IO monad, which ensures actions happen in the right order.

See Also