Operator cheat sheet, and monadic style q

Greetings, 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. 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. I'm curious if 1) anyone has compiled a cheat sheet of common operators and their meanings/modules, in some sort of text-search-friendly format (e.g. spelling out the operator characters, listing alternate names), or 2) a better way for searching for these things? 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 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? :-) Thanks for the help! -- O

Owen Smith wrote:
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. I'm curious if 1) anyone has compiled a cheat sheet of common operators and their meanings/modules, in some sort of text-search-friendly format (e.g. spelling out the operator characters, listing alternate names), or 2) a better way for searching for these things?
Hoogle is more likely to help you here. http://haskell.org/hoogle/

On Sat, Nov 22, 2008 at 11:31 AM, Owen Smith
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
--Max

On Sat, Nov 22, 2008 at 2:31 AM, Owen Smith
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) Luke
participants (4)
-
Andrew Coppin
-
Luke Palmer
-
Max Rabkin
-
Owen Smith