When should I use $ (and can it always be replaced with parentheses)?

前端 未结 5 912
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 21:31

From what I\'m reading, $ is described as \"applies a function to its arguments.\" However, it doesn\'t seem to work quite like (apply ...) in Lisp

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 22:12

    The documentation of ($) answers your question. Unfortunately it isn't listed in the automatically generated documentation of the Prelude.

    However it is listed in the sourcecode which you can find here:

    http://darcs.haskell.org/packages/base/Prelude.hs

    However this module doesn't define ($) directly. The following, which is imported by the former, does:

    http://darcs.haskell.org/packages/base/GHC/Base.lhs

    I included the relevant code below:

    infixr 0  $
    
    ...
    
    -- | Application operator.  This operator is redundant, since ordinary
    -- application @(f x)@ means the same as @(f '$' x)@. However, '$' has
    -- low, right-associative binding precedence, so it sometimes allows
    -- parentheses to be omitted; for example:
    --
    -- >     f $ g $ h x  =  f (g (h x))
    --
    -- It is also useful in higher-order situations, such as @'map' ('$' 0) xs@,
    -- or @'Data.List.zipWith' ('$') fs xs@.
    {-# INLINE ($) #-}
    ($)                     :: (a -> b) -> a -> b
    f $ x                   =  f x
    

提交回复
热议问题