More IO Functions

Get introduced to a few more of the most common `IO` actions from the Prelude, and learn how to combine them with pure code.

Parsing and printing

So far, we have used getLine and putStrLn to read and write lines as strings. But, we will need to read and work with input of other types, such as numbers. In that case, we need to convert them from strings after reading and convert them back to strings before writing.

As we have learned in the lesson on basic data types, we can use the read and show functions for such conversions. Here is a function that reads a number and doubles it.

ioDouble :: IO ()
ioDouble = getLine >>= \s -> putStrLn ((show . (*2) . read) s)

This is clumsy. Thankfully, there are two utility functions from the Prelude that make this easier:

  • readLn combines getLine and read
  • print combines show and putStrLn

Using these, our doubling program becomes easier to read.

ioDouble :: IO ()
ioDouble = readLn >>= \a -> print (a*2)

Reading and writing files

Instead of interfacing with the terminal, we can also read and write files as strings. The IO actions both take a filename as the first argument.

readFile :: String -> IO String
writeFile :: String -> String -> IO ()

Here is a function which copies a file:

copy :: String -> String -> IO ()
copy source dest = readFile source >>= writeFile dest

Note that we are using a partial application of writeFile here. An equivalent, but more verbose version would be

copy :: String -> String -> IO ()
copy source dest = readFile source >>= \s -> writeFile dest s

Get hands-on with 1400+ tech skills courses.