[Haskell-beginners] f . g or f g or f $ g?
es at ertes.de
Wed Feb 13 04:18:08 CET 2013
mukesh tiwari <mukeshtiwari.iiitm at gmail.com> wrote:
> You can write (f . g) x as f . g $ x so for me, it's avoiding extra
Here comes my usual lecture: The ($) operator can be harmful when
explicitly used to avoid extra parentheses, because it makes code flat.
Besides grouping function application parentheses also serve a
readability purpose, about the same one as indentation. In other words,
think twice before you overuse ($).
Background: In some of my projects I have noticed that to revisit old
code I had an easier time rewriting some of my ($) applications back to
parentheses to make the code structure more apparent and easier for the
eye, particularly when both sides of ($) were long and multiple ($)s
were involved. But even in very simple cases parentheses just look much
print $ sin x
print (sin x)
The latter just looks nicer, at least to my eyes. Of course there are
cases when ($) is really preferred, like when one side is very long,
perhaps multi-lined, and the other is very short:
log $ "Client connecting from " ++ hostname
withSteak $ \steak -> do
eatWith cutlery steak
But then of course in many cases you get hungry long before you get the
withSteak (eatWith cutlery)
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 836 bytes
Desc: not available
More information about the Beginners