, compared to $ in haskell

Possible duplicate:
Haskell: the difference between the two. (dot) and $ (dollar sign)

Ok, I understand that this is:

f(g(x)) 

can be rewritten:

 f $ g(x) 

and you can also rewrite:

 f . g(x) 

What I do not fully understand is where the two do not overlap in functionality. I conceptually understand that they are not completely overlapping, but can anyone clarify this for me once and for all?

+7
source share
3 answers
 Prelude> :t ($) ($) :: (a -> b) -> a -> b Prelude> :t (.) (.) :: (b -> c) -> (a -> b) -> a -> c 

$ applies the function to the value. . consists of two functions.

So, I can write f $ gx , which "will apply f to (g of x)" or f . g $ x f . g $ x , which "applies the composition of f and g to x". One common style is to accumulate points to the left with dollar trailing. The reason is that f $ g $ x means the same as f . g $ x f . g $ x , but the expression f $ g itself is often meaningless (in fact, perhaps a type error), and the expression f . g f . g means "composition of f and g"

+21
source

In addition to what has already been said, you need to use $ as a β€œglue application application” in the following cases:

 map ($3) [(4+),(5-),(6*),(+4),(*5),(^6)] --[7,2,18,7,15,729] 

None (.3) and (3) will work in this example.

+14
source

"f $ gx" cannot be rewritten to "f. gx". In fact, the compiler will not even accept the second function, because "(.)" Has the type "(b β†’ c) β†’ (a β†’ b) β†’ (a β†’ c)". I. The second argument must be a function, but "gx" is a value, not a function.

+1
source

All Articles