[Haskell-beginners] f . g or f g or f $ g?

Ertugrul Söylemez 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
> parenthesis.

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
nicer.  Compare

    print $ sin x

with

    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

or:

    withSteak $ \steak -> do
        getHungry
        eatWith cutlery steak
        beHappy

But then of course in many cases you get hungry long before you get the
steak:

    getHungry
    withSteak (eatWith cutlery)
    beHappy


Greets,
Ertugrul

-- 
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...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130213/d281c260/attachment.pgp>


More information about the Beginners mailing list