What is the difference between . (dot) and $ (dollar sign)?

后端 未结 13 2132
庸人自扰
庸人自扰 2020-11-22 04:57

What is the difference between the dot (.) and the dollar sign ($)?

As I understand it, they are both syntactic sugar for not needing to us

13条回答
  •  生来不讨喜
    2020-11-22 05:12

    They have different types and different definitions:

    infixr 9 .
    (.) :: (b -> c) -> (a -> b) -> (a -> c)
    (f . g) x = f (g x)
    
    infixr 0 $
    ($) :: (a -> b) -> a -> b
    f $ x = f x
    

    ($) is intended to replace normal function application but at a different precedence to help avoid parentheses. (.) is for composing two functions together to make a new function.

    In some cases they are interchangeable, but this is not true in general. The typical example where they are is:

    f $ g $ h $ x
    

    ==>

    f . g . h $ x
    

    In other words in a chain of $s, all but the final one can be replaced by .

提交回复
热议问题