Skip to main content

haskell dollar-sign operator

· One min read

The ($) operator is a convenience for when you want to express something with less pairs of parenths.

Type of ($) is:

($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’
infixr 0 $

Which means that:

  1. it is a infix operator
  2. it associates to the right
  3. and, it has the lowest possibile precedence

Usage example:

_ =(2^) (2 + 2) -- 16

Above code we need all the parenths because we want to first evaluate (2 + 2). If we remove the parenths the result is different:

_ = (2^) 2 + 2 -- 6

Here is where ($) comes handy:

_ = (2^) $ 2 + 2 -- 16

This happens because of how ($) associates. So, first (2 + 2) is evaluted and its results is used in (2^).