The >>= function gives you a way to pass a value through some functions while making sure that they are executed in some specific sequence, but sometimes you might find that you don’t actually care about the value.
Imagine that you have several functions that have the type IO () , and you wanted to run them in order. As an example, say we wanted to print several different lines of text to the screen. At first we might try to use >>= to do this, but we’ll get an error:
| | λ putStrLn "hello world" >>= putStrLn "nice to meet you" >>= putStrLn "goodbye" |
| | |
| | <interactive>:16:28: error: |
| | • Couldn't match expected type ‘() -> IO a0’ |
| | with actual type ‘IO ()’ |
| | • Possible cause: ‘putStrLn’ is applied to too many arguments |
| | In the second argument of ‘(>>=)’, namely |
| | ‘putStrLn "nice to meet you"’ |
| | In the first argument of ‘(>>=)’, namely |
| | ‘putStrLn "hello world" >>= putStrLn "nice to meet you"’ |
| | In the expression: |
| | putStrLn "hello world" >>= putStrLn "nice to meet you" |
| | >>= putStrLn "goodbye" |
| | |
| | <interactive>:16:60: error: |
| | • Couldn't match expected type ‘a0 -> IO b’ |
| | with actual type ‘IO ()’ |
| | |
| | • Possible cause: ‘putStrLn’ is applied to too many arguments |
| | In the second argument of ‘(>>=)’, namely ‘putStrLn "goodbye"’ |
| | In the expression: |
| | putStrLn "hello world" >>= putStrLn "nice to meet you" |
| | >>= putStrLn "goodbye" |
| | In an equation for ‘it’: |
| | it |
| | = putStrLn "hello world" >>= putStrLn "nice to meet you" |
| | >>= putStrLn "goodbye" |
| | • Relevant bindings include |
| | it :: IO b (bound at <interactive>:16:1) |
The problem here is that >>= wants something of type a -> IO b , but putStrLn has the type IO () . One way to solve this is to use an intermediate function that ignores its input. You saw an example of this in the last section when you needed to ignore the output of putStrLn so that you could close the file handle. As you can imagine, this is a common situation. For example, we will have the same problem if we want to print two messages to the screen:
| | λ putStrLn "hello world" >>= \_ -> putStrLn "good night world" |
| | hello world |
| | good night world |
This works when we just have one or two functions to join together, but it gets tedious if we have too many things that we want to run in sequence. Imagine if we had to write code like this:
| | showSomeText :: IO () |
| | showSomeText = |
| | putStrLn "this is just some text" |
| | >>= \ _ -> putStrLn "there are many lines of it" |
| | >>= \ _ -> putStrLn "each one a new function" |
You may start wondering if we could write a function to make this pattern less verbose. In fact, we can do just that. Let’s call it thenCall :
| | thenCall :: IO a -> IO b -> IO b |
| | thenCall a b = a >>= \ _ -> b |
| | |
| | showWithThenCall :: IO () |
| | showWithThenCall = |
| | putStrLn "this is just some text" |
| | `thenCall` putStrLn "there are many lines of it" |
| | `thenCall` putStrLn "not one a new function" |
This is a much nicer way of sequencing our IO actions, but it turns out that we’ve once again recreated an existing wheel. The >> function, which also comes from the Monad type class, works exactly like our thenCall function. Let’s look at a final version of our example just to visualize how it works:
| | showWithSeq :: IO () |
| | showWithSeq = |
| | putStrLn "this is just some text" |
| | >> putStrLn "there are many lines of it" |
| | >> putStrLn "not one a new function" |