
On Sun, Oct 30, 2011 at 6:39 PM,
Doesn't . and $ do the same thing? I always get confused about that, like when would I use one over the other.
It is true that there is some overlap, mainly when you have a big stack of one parameter functions to apply :
result = f $ g $ h $ x
or
result = f . g . h $ x
The second form is preferred though for the good reason that it is easier to refactor : You can take any part of the composition chain, pull it out and give it a name (or use it somewhere else) without worry :
result = newFun . h $ x where newFun = f . g
Whereas with the first form you'll need to introduce a point (which you'll have to name) and sometimes rework your expression :
result = newFun $ h $ x where newFun y = f $ g $ y
Anyway as for the difference : f $ g $ h $ x == f (g (h x)) In other words you apply a function to the results of another function itself applied to another function applied to the point x. f . g . h $ x == (f . g . h) x You create a new function that is the composition of the three initial ones then apply it to the point x. Of course in practice the same operation occurs in both cases but conceptually you're more "functional" and "modular" in the second formulation. -- Jedaï