Skip to Content
Effective Haskell
book

Effective Haskell

by Rebecca Skinner
July 2023
Intermediate to advanced
670 pages
17h 13m
English
Pragmatic Bookshelf
Content preview from Effective Haskell

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Grokking Functional Programming

Grokking Functional Programming

Michal Plachta
Grokking Algorithms

Grokking Algorithms

Aditya Bhargava
The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

Steve Klabnik, Carol Nichols

Publisher Resources

ISBN: 9798888650400Errata Page