Chapter 14. Monads
In Chapter 7, we talked about the IO monad, but we intentionally kept the discussion narrowly focused on how to communicate with the outside world. We didn’t discuss what a monad is.
We’ve already seen in Chapter 7 that the IO monad is easy to work with. Notational differences aside, writing code in the IO monad isn’t much different from coding in any other imperative language.
When we had practical problems to solve in earlier chapters, we introduced structures that, as we will soon see, are actually monads. We aim to show you that a monad is often an obvious and useful tool to help solve a problem. We’ll define a few monads in this chapter, to show how easy it is.
Revisiting Earlier Code Examples
Maybe Chaining
Let’s take another look at the parseP5
function that we wrote in Chapter 10:
-- file: ch10/PNM.hs matchHeader :: L.ByteString -> L.ByteString -> Maybe L.ByteString -- "nat" here is short for "natural number" getNat :: L.ByteString -> Maybe (Int, L.ByteString) getBytes :: Int -> L.ByteString -> Maybe (L.ByteString, L.ByteString) parseP5 s = case matchHeader (L8.pack "P5") s of Nothing -> Nothing Just s1 -> case getNat s1 of Nothing -> Nothing Just (width, s2) -> case getNat (L8.dropWhile isSpace s2) of Nothing -> Nothing Just (height, s3) -> case getNat (L8.dropWhile isSpace s3) of Nothing -> Nothing Just (maxGrey, s4) | maxGrey > 255 -> Nothing | otherwise -> case getBytes 1 s4 of Nothing -> Nothing Just (_, s5) -> case getBytes (width * height) s5 of Nothing ...
Get Real World Haskell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.