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.
f :: Either String $ Maybe Int = f :: Either String (Maybe Int)
>>> const 3 $ Prelude.undefined 3 >>> const 3 $! Prelude.undefined *** Exception: Prelude.undefined ...
>>> Nothing $> "foo" Nothing >>> Just 90210 $> "foo" Just "foo"Replace the contents of an Either Int Int with a constant String, resulting in an Either Int String:
>>> Left 8675309 $> "foo" Left 8675309 >>> Right 8675309 $> "foo" Right "foo"Replace each element of a list with a constant String:
>>> [1,2,3] $> "foo" ["foo","foo","foo"]Replace the second element of a pair with a constant String:
>>> (1,2) $> "foo" (1,"foo")
>>> negate <<$>> Just [1,2,3] Just [-1,-2,-3]
>>> 'a' <$ Just 2 Just 'a' >>> 'a' <$ Nothing Nothing
($) :: (a -> b) -> a -> b (<$>) :: Functor f => (a -> b) -> f a -> f bWhereas $ is function application, <$> is function application lifted over a Functor.
>>> show <$> Nothing Nothing >>> show <$> Just 3 Just "3"Convert from an Either Int Int to an Either Int String using show:
>>> show <$> Left 17 Left 17 >>> show <$> Right 17 Right "17"Double each element of a list:
>>> (*2) <$> [1,2,3] [2,4,6]Apply even to the second element of a pair:
>>> even <$> (2,2) (2,True)