Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell function composition (.) and function application ($) idioms: correct use

People also ask

What is function composition in Haskell?

Composing functions is a common and useful way to create new functions in Haskell. Haskell composition is based on the idea of function composition in mathematics. In mathematics, if you have two functions f(x) and g(x), you compute their composition as f(g(x)). The expression f(g(x)) first calls g and then calls f.

What does == mean in Haskell?

The == is an operator for comparing if two things are equal. It is quite normal haskell function with type "Eq a => a -> a -> Bool". The type tells that it works on every type of a value that implements Eq typeclass, so it is kind of overloaded.

Is Haskell left or right associative?

After all most people in the world (including myself) are used to reading from left to right. Nevertheless function composition in Haskell is right associative: infixr 9 .

What does dot mean in Haskell?

The dot ( . ) is the Haskell operator for function composition. Function composition comes from mathematics but actually, it can be really useful to make music. Haskell was originally designed by mathematicians and computer magicians. Its syntax borrowed quite a lot from mathematical notation.


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.


For me, I think the answer is (a) the neatness, as Don said; and (b) I find that when I'm editing code, my function may end up in point-free style, and then all I have to do is delete the last $ instead of going back and changing everything. A minor point, certainly, but a nicety.


There's an interesting discussion of this question on this haskell-cafe thread. Apparently there's a minority viewpoint that holds that the right associativity of $ is "just plain wrong", and choosing f . g . h $ x over f $ g $ h $ x is one way of side-stepping the issue.


It's just a matter of style. However, the way the book does it makes more sense to me. It composes all the functions, and then applies it to the value.

Your method just looks strange, and the last $ is unnecessary.

However, it really doesn't matter. In Haskell, there are usually many, many, correct ways to do the same thing.