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

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

    One application that is useful and took me some time to figure out from the very short description at learn you a haskell: Since:

    f $ x = f x
    

    and parenthesizing the right hand side of an expression containing an infix operator converts it to a prefix function, one can write ($ 3) (4+) analogous to (++", world") "hello".

    Why would anyone do this? For lists of functions, for example. Both:

    map (++", world") ["hello","goodbye"]`
    

    and:

    map ($ 3) [(4+),(3*)]
    

    are shorter than map (\x -> x ++ ", world") ... or map (\f -> f 3) .... Obviously, the latter variants would be more readable for most people.

提交回复
热议问题