In the last section, you learned how we could use a function like andThen :: IO a -> (a -> IO b) -> IO b and newIO :: a -> IO a to work with IO in a pure lazy language. Now you’ll have the opportunity to see how functions like this are implemented, and if you follow along with the example you can write them yourself:
| | andThen :: IO a -> (a -> IO b) -> IO b |
| | andThen = (>>=) |
| | |
| | newIO :: a -> IO a |
| | newIO = return |
That might have been a little bit anticlimactic. It turns out that our implementations are actually just providing new names to the existing >>= operator (pronounced bind ) and the return function.
If you look at the types of (>>=) and return in ghci you’ll see that they are actually more general than just IO . In fact, these functions are defined for the Monad type class. We’ll talk more about monads in Chapter 9, Introducing Monads , but for now let’s just focus on how they are useful for IO specifically.
Before we dive into using >>= and return in a larger example, let’s look at a couple of trivially small examples to get a feel for what it’s like to use them.
Let’s start with a simple function that works like read , but that returns the parsed value inside of an IO action (the built-in readIO function from System.IO provides a more robust version of this, but we’ll stick to our version for the sake of demonstration right now).
| Return and Pure |
|---|
|
| You may run across code that uses a function named pure instead of return . You’ll learn more about the relationship between pure and return later on in this book. For now, it’s enough to know that pure is more general than return , but for IO they’ll behave the same way. |
| | ioRead :: String -> IO Int |
| | ioRead numString = return (read numString) |
We can use this function along with return to turn a String into an IO Int :
| | λ return "4" >>= ioRead |
| | 4 |
| | λ return "10" >>= ioRead |
| | 10 |
We can chain more than two functions together. If we write another function to take our IO Int and turn it back into an IO String then we can translate back and forth as many times as we want:
| | ioShow :: Int -> IO String |
| | ioShow = return . show |
Again, let’s go back over to our REPL and test out our new function. Now that we can go from a string to an int and back, we can join our functions together as much as we want:
| | λ return "4" >>= ioRead >>= ioShow >>= ioRead >>= ioShow |
| | "4" |
| | λ return "4" >>= ioRead >>= ioShow >>= ioRead >>= ioShow >>= ioRead |
| | 4 |
Adding more and more conversions between an int and a string is a pretty good way to waste energy if your office is getting a bit chilly, but it’s hard to really know that we’re actually doing anything since we’re always getting back the same value we put in. Let’s write another function that will let us increment our integer. We’ll use the succ function from Prelude to increment our number:
| | ioSucc :: Int -> IO Int |
| | ioSucc = return . succ |
| | λ return "1" >>= ioRead >>= ioSucc |
| | 2 |
| | λ return "1" >>= ioRead >>= ioSucc >>= ioSucc >>= ioShow |
| | "3" |
Look! We’ve just invented addition! As neat as addition is though, let’s turn our attention to a more practically useful example: printing the contents of a file to the screen. We’ll use a couple of functions from System.IO that we glossed over in the last section:
-
openFile :: FilePath -> IOMode -> IO Handle
takes a path to a file, and a mode like ReadMode or WriteMode , and returns a Handle to a file
-
hClose :: Handle -> IO ()
Closes a file Handle . You should use hClose to close every handle that you open with openFile . There are a number of utility functions to manage this for you.
-
hGetContents :: Handle -> IO String
takes a file handle and returns a string with the contents of that file
We’ll also be using putStrLn :: String -> IO () to write the contents of the string to the screen.
Now that you understand how >>= works and how to chain together IO commands, it’s not to hard to follow a real working example of how to print a file’s contents to the screen:
| | doSomeFileStuff = |
| | openFile "/tmp/foo.txt" ReadMode |
| | >>= \ handle -> hGetContents handle |
| | >>= \ contents -> putStrLn contents |
| | >>= \ _ -> hClose handle |