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

后端 未结 13 1993
庸人自扰
庸人自扰 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:03

    Also note that ($) is the identity function specialised to function types. The identity function looks like this:

    id :: a -> a
    id x = x
    

    While ($) looks like this:

    ($) :: (a -> b) -> (a -> b)
    ($) = id
    

    Note that I've intentionally added extra parentheses in the type signature.

    Uses of ($) can usually be eliminated by adding parenthesis (unless the operator is used in a section). E.g.: f $ g x becomes f (g x).

    Uses of (.) are often slightly harder to replace; they usually need a lambda or the introduction of an explicit function parameter. For example:

    f = g . h
    

    becomes

    f x = (g . h) x
    

    becomes

    f x = g (h x)
    

    Hope this helps!

提交回复
热议问题