CharlesS
CharlesS

Reputation: 1591

What does the >> symbol mean in Haskell

I was reading the Guestbook example for Happstack and noticed the >> symbol which I didn't see before in the textbooks I studied to learn Haskell (for instance see line 23). What is it?

I could not find it in Google because it ignores the >> totally (Bing does not but comes up with tons of non-related results).

Upvotes: 40

Views: 26629

Answers (5)

Sevan Golnazarian
Sevan Golnazarian

Reputation: 987

From Hackage, >> is described as:

"Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages."

I think a good example is printing two strings sequentially using >>. Open GHCI and type the following:

putStr "Hello " >> putStrLn "World"

This is equivalent to the do notation:

do putStr "Hello "
   putStrLn "World"

Upvotes: 11

kennytm
kennytm

Reputation: 523214

In do-notation

a >> b >> c >> d

is equivalent to

do a
   b
   c
   d

(and similarly a >>= (b >>= (c >>= d)) is equivalent to

do r1 <- a
   r2 <- b r1
   r3 <- c r2
   d r3

Upvotes: 47

j_random_hacker
j_random_hacker

Reputation: 51226

I'm no Haskell expert, but >> is an operator that is used for working with monads, which are an unusual feature that (among many other things) enable imperative-style programming in Haskell. There are many tutorials available on monads; here's one good one.

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 >>=.

Upvotes: 5

jrockway
jrockway

Reputation: 42674

At the ghci command prompt, you can type:

:info >>

And get a result like:

class Monad m where
...
(>>) :: m a -> m b -> m b
...
        -- Defined in GHC.Base
infixl 1 >>

From there, you can just take a look at the source code to learn more.

And just for the sake of answering your question:

k >> f = k >>= \_ -> f

Upvotes: 19

Tim Robinson
Tim Robinson

Reputation: 54724

Hayoo recognises this kind of operator: http://holumbus.fh-wedel.de/hayoo/hayoo.html

(>>) is like (>>=), in that it sequences two actions, except that it ignores the result from the first one.

Upvotes: 25

Related Questions