Mapping Functors

A Functor is a simple type class that has just two functions, fmap and <$. As you’ll see throughout the rest of this book, Functor instances are extremely common in Haskell, and you’re likely to work with this type class in nearly every program you write. Before we dive into what a Functor is, let’s take a look at how we could define the type class ourselves if it wasn’t already provided for us in base:

 class​ ​Functor​ f ​where
  fmap :: (a -> b) -> f a -> f b
  (<$) :: a -> f b -> f a
  (<$) a fb = fmap (const a) fb

In addition to the functions defined in the type class, the <$> function is also defined in the standard library as an infix version of fmap. In most Haskell code, you’ll see <$> being used at least as often ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.