`h $ g $ f x y z` versus `h . g $ f x y z`

I sometimes see h $ g $ f x y z and other times see h . g $ f x y z. I understand that the former is equivalent to h (g (f x y z)) and the latter equivalent to (\p -> h(g p))(f x y z).

Is it possible to reason that one is to be preferred over the other? If so, what is that reasoning?

1 Like

Mostly style preference, and the fact that ormolu formats the latter to be better-looking. I prefer the former one though.

1 Like

h . g $ f x y z

↑ best one for me. The . points my attention at “this is pointfree, consider it as one”, is clearer than the dollar. I usually see/use $ as “save you matching some parentheses”.

2 Likes

The reason people prefer chains of composition is, e.g., to use your example, h $ g is not a type-correct and meaningful unit on its own, while h . g is. So the latter encourages building compositional units which are easier to refactor, substitute, and shuffle around.

2 Likes

I prefer $ a lot of times. A fuzzy rule of thumb is: if you can’t give h . g a short descriptive name, then it’s more chaining than composition.

I don’t like . as a “chaining” operator.

1 Like

I guess is about personal preference. I do prefer h . g $ f x y z because

  • It is slightly easier to refactor
some_thing = h . g $ f x y z 
some_thing = expressive_name $ f x y z   
  where expressive_name   = h . g    -- This is just copy-paste

some_thing' = h $ g $ f x y z 
some_thing' = expressive_name $ f x y z   
  where expressive_name   = h . g    -- You have to change the $ by . or
        expressive_name x = h $ g x  -- You have to add the extra x
  • and easier to read
some =  h . g $ f x y z     -- you can focus on the two importan parts function - argument
--        |   |- applied to 
--        |- some func

some =  h $ g $ f x y z     -- less clear what it means in my opinion
--        |   |- applied to 
--        |- applied to 

Clearly a too long post for something not important… :sweat_smile:

1 Like