[Haskell-beginners] What's the mean of >>=

Alexey Shmalko rasen.dubi at gmail.com
Sun Apr 19 23:51:33 UTC 2015


Hello!

>>= and >> are methods of Monad typeclass. Their meaning may be very different depending on the actual type. For IO >>= composes a new IO action which is, when executed, executes left action first and then passes its result to the right function.

So you can read
putStr' xs >>= \ x -> putChar '\n'
as "Execute action putStr' xs, then pass its result to \x -> putChar '\n'".

Many times the result of previous action is not used (as in above
example), so there is >> which is like >>= but ignores the result of
first action.
It's something like
x >> y = x >>= (\_ -> y)

So putStr' xs >> putChar '\n' is the same as putStr' xs >>= \ x -> putChar '\n'

This code can be rewritten in the do-notation as:
do
    putStr' xs
    putChar '\n'

To summarize, for IO, >>= and >> are used to sequence actions.

Best regards,
Alexey Shmalko

On Mon, Apr 20, 2015 at 2:33 AM, 김명신 <himskim at msn.com> wrote:
> It could be stupid question because i'm very beginner of haskell.
>
> I found strange literals when i read haskell code.
> Does anybody explan what the mean of >>= below ocode ? And additionally,
> What is mean of >> itself?
>
> putStrLn' [] = putChar '\n'
> putStrLn' xs - putStr' xs >>= \ x -> putChar '\n'
>
> putStr' [] = return ()
> putStr' (x : xs) = putChar x >> putStr' n
>
> (because of i18n problem, back slash literal could be shown as '\')
>
> Thank you in advance
> Myung Shin Kim
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>


More information about the Beginners mailing list