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

Max Rabkin max.rabkin at gmail.com
Sat Nov 22 04:42:01 EST 2008

On Sat, Nov 22, 2008 at 11:31 AM, Owen Smith <ods94065 at gmail.com> wrote:
> I'm a longtime Haskell-curious programmer who, after a few aborted
> attempts at getting started and long nights staring at academic
> papers, finally managed to get the bug. I've been pleased with my
> progress so far, but a couple of things have bugged me enough to seek
> advice from the rest of y'all.

Welcome to our addiction!

> 1. Contending with the use of frequently unfamiliar non-alphanumeric
> operators has been an uphill battle for me. I think the main reason
> for this is that I've had no luck in Googling up their definitions (my
> primary approach for dealing with every other unknown in the Haskell
> universe) due to their very non-alphanumeric nature.

The solution is Hoogle (http://haskell.org/hoogle/). Hoogle allows
searching not only alphanumeric operators, but all of the standard
Haskell libraries, as well as some non-standard libraries on Hackage.
You can search by name, package, module or type. Results link to
Haddock documentation, which usually links in turn to source code.

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

I also started out with too many parentheses. Don't let them bother
you too much. Over time, they'll diminish.

> 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

I think it's my fave too.

> baz = bar <=< foo
> Yeah, so "there's more than one way to do it"--though I would never
> have known about =<<, >=>, and <=< from looking at the introductory
> material I've seen on the subject. What do people here prefer using in
> what circumstances? Or is everyone off using do notation or arrows
> instead? :-)

I tend to use arrow or applicative notation where it applies, simply
because it's more general and sometimes prettier. Use do notation when
it's the easiest to understand (this will probably also diminish over
time). I generally only use it when there is complex sequencing, and
certainly not in this case.

> Thanks for the help!
> -- O


More information about the Haskell-Cafe mailing list