O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

In this recipe, we created an instance of Functor for the binary tree that we defined. When we create an instance of a Functor, we have to follow these laws of Functor:

Laws of Functor An application of an identity function id should get the same data back: fmap id == id Applying two functions to the same data type in a sequence should be the same as the application of a composition of two functions: fmap (p . q) == fmap p . fmap q

For the binary tree that we have defined, these laws are followed. If we apply the identity function id, then we apply it to the value in the node and subtrees. Since id does not change the value and we preserve the structure of the tree, an application of an identity function to our binary ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required