Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand Monads. The >> Operator

I'm new to haskell and I'm learning through LearnYouAHaskell. I just can't understand the reason behind the (>>) operator.
The default implementation is:

(>>) :: (Monad m) => m a -> m b -> m b  
m >> n = m >>= \_ -> n 

Which is (as far as I understand) ignores the first value and returns the second one. However, from the example in LearnYouAHaskell this happens:

ghci> Nothing >> Just 3
Nothing
ghci> Just 3 >> Nothing
Nothing

So it doesn't ignores the first value. Yet, from a little research I found this quote from here

The >> function binding operator ignores the value of its first action and returns as an overall result the result of its second action only.

So I'm puzzled about the usage of this operator and I want to ask 2 things:

  1. What does it actually do?
  2. When is it useful?
like image 475
Tzah Mama Avatar asked Jul 20 '14 05:07

Tzah Mama


People also ask

What does >> mean in Haskell?

Essentially, a >> b can be read like "do a then do b , and return the result of b ". It's similar to the more common bind operator >>= .

What are monads explain with example?

In functional programming, a monad is a software design pattern with a structure that combines program fragments (functions) and wraps their return values in a type with additional computation.

How do monads work?

So in simple words, a monad is a rule to pass from any type X to another type T(X) , and a rule to pass from two functions f:X->T(Y) and g:Y->T(Z) (that you would like to compose but can't) to a new function h:X->T(Z) . Which, however, is not the composition in strict mathematical sense.

How do monads work Haskell?

A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.


2 Answers

The >> function only ignores the result of the first value but it doesn't ignore the side effect of the first value. To understand your example, see how Maybe Monad is defined:

instance Monad Maybe where
  return = Just
  (Just x) >>= f = f x
  Nothing >>= _ = Nothing

And >> function is defined like this:

m >> k      = m >>= \_ -> k

Nothing >> _ will produce Nothing according to the definition of Maybe monad. In your second example Just 3 >> Nothing gets expanded to Just 3 >>= \_ -> Nothing and produces Nothing. To give you an example of how it only ignores the value of the first action but doesn't ignore the side effect, consider the following example:

λ> print 3 >> print 4
3
4

You can see in the above example, that although it ignores the result of print 3 which is () but it doesn't ignore the side effect of it which is to display 3 to the screen.

>> function becomes useful once you start using other Haskell libraries. Two places where I use them occasionally is when dealing with Parsers (parsec, attoparsec) and Pipes library.

like image 146
Sibi Avatar answered Oct 21 '22 02:10

Sibi


It ignores the value of the first action, not the action itself.

Just 3 >> Just 5

The value of the action Just 3 is 3. It is being ignored in the \_ -> n part. The overall result is Just 5.

Just 3 >> Nothing

The value of the action Just 3 is 3. It is being ignored in the \_ -> n part. The overall result is Nothing.

Nothing >> Just 3

The action Nothing does not produce any value at all. What does it pass to the right operand of >>= (or >>) then? It doesn't! >>= for the Maybe monad is built such that if the left action is Nothing, the right action is not executed at all, and the overall result is Nothing.

like image 43
n. 1.8e9-where's-my-share m. Avatar answered Oct 21 '22 03:10

n. 1.8e9-where's-my-share m.