Online Book Reader

Home Category

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

By Root 574 0
If it isn’t, we return a Nothing, indicating failure.

Let’s give these babies a go:

ghci> landLeft 2 (0, 0)

Just (2,0)

ghci> landLeft 10 (0, 3)

Nothing

When we land birds without throwing Pierre off balance, we get a new pole wrapped in a Just. But when many more birds end up on one side of the pole, we get a Nothing. This is cool, but we seem to have lost the ability to repeatedly land birds on the pole. We can’t do landLeft 1 (landRight 1 (0, 0)) anymore, because when we apply landRight 1 to (0, 0), we don’t get a Pole, but a Maybe Pole. landLeft 1 takes a Pole, rather than a Maybe Pole.

We need a way of taking a Maybe Pole and feeding it to a function that takes a Pole and returns a Maybe Pole. Luckily, we have >>=, which does just that for Maybe. Let’s give it a go:

ghci> landRight 1 (0, 0) >>= landLeft 2

Just (2,1)

Remember that landLeft 2 has a type of Pole -> Maybe Pole. We couldn’t just feed it the Maybe Pole that is the result of landRight 1 (0, 0), so we use >>= to take that value with a context and give it to landLeft 2. >>= does indeed allow us to treat the Maybe value as a value with context. If we feed a Nothing into landLeft 2, the result is Nothing, and the failure is propagated:

ghci> Nothing >>= landLeft 2

Nothing

With this, we can now chain landings that may fail, because >>= allows us to feed a monadic value to a function that takes a normal one. Here’s a sequence of bird landings:

ghci> return (0, 0) >>= landRight 2 >>= landLeft 2 >>= landRight 2

Just (2,4)

At the beginning, we used return to take a pole and wrap it in a Just. We could have just applied landRight 2 to (0, 0)—it would have been the same—but this way, we can be more consistent by using >>= for every function. Just (0, 0) is fed to landRight 2, resulting in Just (0, 2). This, in turn, gets fed to landLeft 2, resulting in Just (2, 2), and so on.

Remember the following example from before we introduced failure into Pierre’s routine?

ghci> (0, 0) -: landLeft 1 -: landRight 4 -: landLeft (-1) -: landRight (-2)

(0,2)

It didn’t simulate his interaction with birds very well. In the middle, his balance was off, but the result didn’t reflect that. Let’s fix that now by using monadic application (>>=) instead of normal application:

ghci> return (0, 0) >>= landLeft 1 >>= landRight 4 >>= landLeft (-1) >>= landRight (-2)

Nothing

The final result represents failure, which is what we expected. Let’s see how this result was obtained:

return puts (0, 0) into a default context, making it a Just (0, 0).

Just (0, 0) >>= landLeft 1 happens. Since the Just (0, 0) is a Just value, landLeft 1 gets applied to (0, 0), resulting in a Just (1, 0), because the birds are still relatively balanced.

Just (1, 0) >>= landRight 4 takes place, and the result is Just (1, 4), as the balance of the birds is still intact, although just barely.

Just (1, 4) gets fed to landLeft (-1). This means that landLeft (-1) (1, 4) takes place. Now because of how landLeft works, this results in a Nothing, because the resulting pole is off balance.

Now that we have a Nothing, it gets fed to landRight (-2), but because it’s a Nothing, the result is automatically Nothing, as we have nothing to apply landRight (-2) to.

We couldn’t have achieved this by just using Maybe as an applicative. If you try it, you’ll get stuck, because applicative functors don’t allow for the applicative values to interact with each other very much. They can, at best, be used as parameters to a function by using the applicative style.

The applicative operators will fetch their results and feed them to the function in a manner appropriate for each applicative, and then put the final applicative value together, but there isn’t that much interaction going on between them. Here, however, each step relies on the previous one’s result. On every landing, the possible result from the previous one is examined and the pole is checked for balance. This determines whether the landing will succeed or fail.

Banana on a Wire


Now let’s devise a function that ignores the current

Return Main Page Previous Page Next Page

®Online Book Reader