39

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

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
CharlesS
  • 1,563
  • 2
  • 18
  • 31
  • 4
    hoogle (and hayoo mentioned below) are the best way to search using haskell syntax http://www.haskell.org/hoogle/?hoogle=%3E%3E – Keith Feb 27 '10 at 18:42
  • There's also Google Code Search which is generally good for cases like this although it may not be the best for this particular case: http://google.com/codesearch?q=lang%3Ahaskell+%3E%3E&hl=en&btnG=Search+Code – Tyler Feb 28 '10 at 05:46

5 Answers5

47

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
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • As Haskell functions are generally pure, it appears to me that there is no point in evaluating a, b, c, or d in the above example, so Haskell wouldn't do it unless it contains side effects (IO etc), is this reasoning correct? – Nearoo Aug 09 '20 at 10:46
  • 2
    @Nearoo Not an expert on the topic, but I think you've overlooked that a, b, c or d could be pure (monads) yet still throw an exception, thereby affecting program flow even though their return values are ignored. – voneiden Nov 23 '20 at 19:39
25

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.

Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • 2
    The ultimate fist-fighting championship dukeout: Hoogle vs. Hayoo! – Thomas Eding Feb 27 '10 at 19:39
  • Thanks for the memorable URL! I've been accessing the site via http://hackage.haskell.org. – Tim Robinson Feb 28 '10 at 10:24
  • Considering Haskell evaluates lazily, does this result in no evaluation of the function on the left of `>>` at all? – Nearoo Aug 09 '20 at 10:48
  • @Nearoo: no, those are actions (monadic values), so they have side effects. – Seweryn Niemiec Oct 01 '21 at 19:59
  • @SewerynNiemiec The side effect point probably only matters in the IO monad case? – Hans Feb 10 '22 at 19:32
  • @Hans No, it matters for every monad I believe. The main point of monads in Haskell is to denote a sequence of operations, which would otherwise be indeterminate with lazy evaluation. With the `Maybe` monad, `Just 1 >> Nothing >> Just undefined` results in `Nothing`, with `Either`, `Right True >> Left "err" >> Left undefined` results in `Left "err"`, with the list monad, `[1,2,3] >> [] >> [1..]` results in `[]`, and with `State`, `let f = get >>= put . succ in execState (f >> f >> f) 0` results in `3`. So any "state" may still get updated, and any "exception" stops further evaluation. – lvsz Aug 17 '22 at 21:50
18

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
jrockway
  • 42,082
  • 9
  • 61
  • 86
  • 2
    I feel stupid now; I know about the :i (info) command but I come from Java and PHP and still am getting used to using REPL for everything. Thanks – CharlesS Feb 27 '10 at 10:02
  • 40
    Correct but barely helpful to a Haskell newbie. Why does it exist? What is it useful for? +0. – j_random_hacker Feb 27 '10 at 10:02
  • 2
    This is enough for me to find out where to search ; I didn't have any clue before. – CharlesS Feb 27 '10 at 10:05
11

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"

5

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

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • 9
    Answering with "which are difficult-to-understand things" doesn't clarify. It sets the reader up for failure, I think. – Don Stewart Feb 27 '10 at 18:03
  • Fair enough. They are now "an unusual feature" instead of being "difficult-to-understand things." – j_random_hacker Feb 28 '10 at 05:51
  • It's "an unusual feature" to call a function and pass the result to another function? OK... – jrockway Mar 04 '10 at 23:30
  • 1
    You're describing either plain composition of functions, which has nothing to do with monads, or a continuation, and yes those are also difficult for those coming from the imperative world to understand. All that aside: what Haskell topic has produced the greatest number of tutorials and newbie guides (often of the form "A Monad Is Like an X")? Why is that? – j_random_hacker Mar 05 '10 at 01:30
  • 1
    From a newbie persepective I found this answer informative. Monads are definitely a learning curve (aka unusual feature) for those new to functional programming – vikingsteve Dec 22 '15 at 11:32