Haskell function composition (.) and function application ($) idioms: correct use
I guess I can answer this from authority.
Is there a reason for using the books way that is much better than using all ($) symbols?
There's no special reason. Bryan and I both prefer to reduce line noise. .
is quieter than $
. As a result, the book uses the f . g . h $ x
syntax.
They are indeed equivalent: Keep in mind that the $
operator does, essentially, nothing. f $ x
evaluates to f x
. The purpose of $
is its fixity behavior: right-associative and minimal precedence. Removing $
and using parentheses for grouping instead of infix precedence, the code snippets look like this:
k = a (b (c (value)))
and
k = (a . b . c) value
The reason for preferring the .
version over the $
version is the same reason for preferring both over the very parenthesized version above: aesthetic appeal.
Although, some might wonder if using infix operators instead of parentheses is based on some subconscious urge to avoid any possible resemblance to Lisp (just kidding... I think?).
I'd add that in f . g $ x
, f . g
is a meaningful syntactic unit.
Meanwhile, in f $ g $ x
, f $ g
is not a meaningful unit. A chain of $
is arguably more imperative -- first get the result of g
of x
, then do f
to it, then do foo
to it, then etc.
Meanwhile a chain of .
is arguably more declarative, and in some sense closer to a dataflow centric view -- compose a series of functions, and ultimately apply them to something.