f $ g $ h x = f (g (h x))It is also useful in higher-order situations, such as map ($ 0) xs, or zipWith ($) fs xs. Note that ($) is representation-polymorphic in its result type, so that foo $ True where foo :: Bool -> Int# is well-typed.
($) :: (a -> b) -> a -> b ($) f x = f xThis is id specialized from a -> a to (a -> b) -> (a -> b) which by the associativity of (->) is the same as (a -> b) -> a -> b. On the face of it, this may appear pointless! But it's actually one of the most useful and important operators in Haskell. The order of operations is very different between ($) and normal function application. Normal function application has precedence 10 - higher than any operator - and associates to the left. So these two definitions are equivalent:
expr = min 5 1 + 5 expr = ((min 5) 1) + 5($) has precedence 0 (the lowest) and associates to the right, so these are equivalent:
expr = min 5 $ 1 + 5 expr = (min 5) (1 + 5)
-- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum (mapMaybe readMaybe (words s))we can deploy the function application operator:
-- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum $ mapMaybe readMaybe $ words s($) is also used as a section (a partially applied operator), in order to indicate that we wish to apply some yet-unspecified function to a given value. For example, to apply the argument 5 to a list of functions:
applyFive :: [Int] applyFive = map ($ 5) [(+1), (2^)] >>> [6, 32]
fastMod :: Int -> Int -> Int fastMod (I# x) (I# m) = I# $ remInt# x m
f $ g $ h x = f (g (h x))It is also useful in higher-order situations, such as map ($ 0) xs, or zipWith ($) fs xs. Note that ($) is levity-polymorphic in its result type, so that foo $ True where foo :: Bool -> Int# is well-typed.
f $ g $ h x = f (g (h x))It is also useful in higher-order situations, such as map ($ 0) xs, or zipWith ($) fs xs.
f $ g $ h x = f (g (h x))It is also useful in higher-order situations, such as map ($ 0) xs, or zipWith ($) fs xs. Note that ($) is levity-polymorphic in its result type, so that foo $ True where foo :: Bool -> Int# is well-typed
>>> print <| negate <| recip <| succ <| 3 -0.25Or use it anywhere you would use ($). Note that (<|) and (|>) have the same precedence, so they cannot be used together.
>>> -- This doesn't work! >>> -- print <| 3 |> succ |> recip |> negate
\ x -> (f <| x) == f x
\ x -> (g <| f <| x) == g (f x)
>>> print <! negate <! recip <! succ <! 3 -0.25Or use it anywhere you would use ($!). The difference between this and (<|) is that this evaluates its argument before passing it to the function.
>>> const True <| undefined True >>> const True <! undefined *** Exception: Prelude.undefined ...Note that (<!) and (!>) have the same precedence, so they cannot be used together.
>>> -- This doesn't work! >>> -- print <! 3 !> succ !> recip !> negate
\ x -> (f <! x) == seq x (f x)
\ x -> (g <! f <! x) == let y = seq x (f x) in seq y (g y)
($) :: (a -> b) -> a -> b ($) f x = f xThis is id specialized from a -> a to (a -> b) -> (a -> b) which by the associativity of (->) is the same as (a -> b) -> a -> b. On the face of it, this may appear pointless! But it's actually one of the most useful and important operators in Haskell. The order of operations is very different between ($) and normal function application. Normal function application has precedence 10 - higher than any operator - and associates to the left. So these two definitions are equivalent:
expr = min 5 1 + 5 expr = ((min 5) 1) + 5($) has precedence 0 (the lowest) and associates to the right, so these are equivalent:
expr = min 5 $ 1 + 5 expr = (min 5) (1 + 5)
-- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum (mapMaybe readMaybe (words s))we can deploy the function application operator:
-- | Sum numbers in a string: strSum "100 5 -7" == 98 strSum :: String -> Int strSum s = sum $ mapMaybe readMaybe $ words s($) is also used as a section (a partially applied operator), in order to indicate that we wish to apply some yet-unspecified function to a given value. For example, to apply the argument 5 to a list of functions:
applyFive :: [Int] applyFive = map ($ 5) [(+1), (2^)] >>> [6, 32]
fastMod :: Int -> Int -> Int fastMod (I# x) (I# m) = I# $ remInt# x m