← All languages
Haskell function of the day
Random

<$>

Infix synonym for fmap, applying a function inside a Functor.

Description

The (<$>) operator is the infix form of fmap. Its type signature is Functor f => (a -> b) -> f a -> f b. It applies a plain function to a value inside any Functor, including Maybe, Either, lists, and IO.

(<$>) is one of the most commonly used operators in modern Haskell. The expression f <$> x is identical to fmap f x but reads more naturally in chains, especially when followed by (<*>) for applicative-style computations: f <$> x <*> y <*> z.

For IO actions, length <$> getLine reads a line and returns its length without binding the intermediate string. For Maybe, (+1) <$> Just 5 returns Just 6, and for lists, (*2) <$> [1,2,3] returns [2,4,6]. The visual symmetry with (<*>) makes applicative composition particularly elegant.

Arguments

NameDescriptionOptional
f A pure function (a -> b) to apply inside the Functor. No
fa A Functor value (f a) to map over. No

Example

(+1) <$> Just 5            -- Just 6
show <$> [1, 2, 3]         -- ["1","2","3"]
length <$> getLine          -- IO Int counting characters
(*) <$> Just 3 <*> Just 4   -- Just 12

Reference