Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is dollar operator ($) considered bad form? Why?

In Haskell, I often write expressions with $'s. I find it quite natural and readable, but I sometimes read it is bad form and do not understand why it should be.

like image 355
luispedro Avatar asked Dec 23 '11 17:12

luispedro


1 Answers

The following are all good form:

foo = bar . baz . quux
foo x = bar . baz . quux $ x
foo x = (bar . baz . quux) x
foo x = bar (baz (quux x))

I've put them in rough order of my preference, though as always taste varies, and context may demand a different choice. I've also occasionally seen

foo = bar
    . baz
    . quux

when each of the bar, baz, and quux subexpressions are long. The following is bad form:

foo x = bar $ baz $ quux $ x

There are two reasons this is less preferable. First, fewer subexpressions can be copied and pasted into an auxiliary definition during refactoring; with all ($) operators, only subexpressions that include the x argument are valid refactorings, whereas with (.) and ($) operators even subexpressions like bar . baz or baz . quux can be pulled out into a separate definition.

The second reason to prefer (.) is in anticipation of a possible change to the fixity of ($); currently, ($) is infixr, meaning it associates to the right, like so:

foo x = bar $ (baz $ (quux $ x))

However, ($) would be useful in more expressions if it were infixl; for example, something like

foo h = f (g x) (h y)
foo h = f $ g x $ h y
foo h = (f $ g x) $ h y

...which currently cannot be expressed without parentheses. The "bad form" example, when parsed with an infixl application, would be

foo x = ((bar $ baz) $ quux) $ x

which means something significantly different. So, future-proof your code by avoiding this form.

like image 139
Daniel Wagner Avatar answered Sep 21 '22 04:09

Daniel Wagner