Online Book Reader

Home Category

Learn You a Haskell for Great Good! - Miran Lipovaca [135]

By Root 517 0
let f x = [x,-x]

ghci> let g x = [x*3,x*2]

ghci> let h = f <=< g

ghci> h 3

[9,-9,6,-6]

Okay, that’s cool. But what does that have to do with the associativity law? Well, when we look at the law as a law of compositions, it states that f <=< (g <=< h) should be the same as (f <=< g) <=< h. This is just another way of saying that for monads, the nesting of operations shouldn’t matter.

If we translate the first two laws to use <=<, then the left identity law states that for every monadic function f, f <=< return is the same as writing just f. The right identity law says that return <=< f is also no different from f. This is similar to how if f is a normal function, (f . g) . h is the same as f . (g . h), f . id is always the same as f, and id . f is also just f.

In this chapter, we took a look at the basics of monads and learned how the Maybe monad and the list monad work. In the next chapter, we’ll explore a whole bunch of other cool monads, and we’ll also make our own.

Chapter 14. For a Few Monads More

You’ve seen how monads can be used to take values with contexts and apply them to functions, and how using >>= or do notation allows you to focus on the values themselves, while Haskell handles the context for you.

You’ve met the Maybe monad and seen how it adds a context of possible failure to values. You’ve learned about the list monad and seen how it lets us easily introduce nondeterminism into our programs. You’ve also learned how to work in the IO monad, even before you knew what a monad was!

In this chapter, we’ll cover a few other monads. You’ll see how they can make your programs clearer by letting you treat all sorts of values as monadic ones. Further exploration of monads will also solidify your intuition for recognizing and working with monads.

The monads that we’ll be exploring are all part of the mtl package.(A Haskell package is a collection of modules.) The mtl package comes with the Haskell Platform, so you probably already have it. To check if you do, type ghc-pkg list from the command line. This will show which Haskell packages you have installed, and one of them should be mtl, followed by a version number.

Writer? I Hardly Knew Her!


We’ve loaded our gun with the Maybe monad, the list monad, and the IO monad. Now let’s put the Writer monad in the chamber and see what happens when we fire it!

Whereas the Maybe monad is for values with an added context of failure, and the list monad is for nondeterministic values, the Writer monad is for values that have another value attached that acts as a sort of log value. Writer allows us to do computations while making sure that all the log values are combined into one log value, which then is attached to the result.

For instance, we might want to equip our values with strings that explain what’s going on, probably for debugging purposes. Consider a function that takes a number of bandits in a gang and tells us if that’s a big gang. It’s a very simple function:

isBigGang :: Int -> Bool

isBigGang x = x > 9

Now, what if instead of just giving us a True or False value, we want the function to also return a log string that says what it did? Well, we just make that string and return it alongside our Bool:

isBigGang :: Int -> (Bool, String)

isBigGang x = (x > 9, "Compared gang size to 9.")

So now, instead of just returning a Bool, we return a tuple, where the first component of the tuple is the actual value and the second component is the string that accompanies that value. There’s some added context to our value now. Let’s give this a go:

ghci> isBigGang 3

(False,"Compared gang size to 9.")

ghci> isBigGang 30

(True,"Compared gang size to 9.")

So far, so good. isBigGang takes a normal value and returns a value with a context. As you’ve just seen, feeding it a normal value is not a problem. Now what if we already have a value that has a log string attached to it, such as (3, "Smallish gang."), and we want to feed it to isBigGang? It seems like once again, we’re faced with this question: If we have a function that takes a normal value and

Return Main Page Previous Page Next Page

®Online Book Reader