[Haskell-cafe] Operator cheat sheet, and monadic style q

Luke Palmer lrpalmer at gmail.com
Sat Nov 22 04:50:34 EST 2008

On Sat, Nov 22, 2008 at 2:31 AM, Owen Smith <ods94065 at gmail.com> wrote:
> 2. There's a lot I need to learn about good Haskell style, especially
> coming from a C++ background. Even my experience in Lisp seems to
> result in way more parentheses than Haskell coders are comfortable
> with. :-) In particular, I'm curious about how people actually use
> monadic operators. The following simple forms with the Maybe monad,
> for example, appear to be equivalent (hope I and QuickCheck are right
> about that!):
> foo :: Int -> Maybe Int
> bar :: Int -> Maybe Int
> baz :: Int -> Maybe Int
> baz n = (foo n) >>= bar
> baz n = bar =<< (foo n)
> baz n = (foo >=> bar) n
> baz n = (foo <=< bar) n
> and I'm thinking the latter two are more idiomatically written as:
> baz = foo >=> bar -- I think this one is my fave, naively speaking
> baz = bar <=< foo

You will find many differing opinions about this; just pick one that
you find pretty and elegant.

My personal style is never to use >>= or >=>; only =<< and <=<.  These
operators are for when I want monadic operations to feel like function
application, which has the data flowing right to left.  When I want
monadic operations to feel like sequencing, I always use do notation.

This is a particular case of my general inclination to have data in my
progarm flow from top to bottom and right to left.  (However I still
often "read" it left to right; but I am inconsistent about that, and
often switch back and forth when trying to comprehend a piece of code)


More information about the Haskell-Cafe mailing list