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

Using the Functor Laws

Functors are the simplest of our trio of type classes, and also have the simplest laws, so we’ll start with them. There are two laws that well-behaved functors should adhere to, and they are both documented in the standard library documentation. There are two functor laws, identity and composition:

 -- Identity
 fmap id = id
 -- Composition
 fmap (f . g) = fmap f . fmap g

The identity law of functors is pretty simple: mapping the identity function shouldn’t change the value of the functor. The implication is that fmap should only ever be a mapping function and shouldn’t change anything in the functor except for the value that it’s mapping. The second law, composition, follows naturally from the first. The law of composition ...

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