In the last recipe, we have Functor instances defined for Maybe, Either, and List. We even defined the Functor instance. In this recipe, we will create a data type Binary Tree and define a Functor instance for it.
Binary tree as Functor
How to do it...
- Create a new Haskell project binary-tree-functor using the simple Stack template:
stack new binary-tree-functor simple
- Open src/Main.hs. This is the file that will be used for our purposes.
- After adding module definition for Main, add the following import:
module Main where
import Data.Functor
- Define the binary tree and utility functions to create the tree:
-- The tree can be empty (Leaf) or a node with a
-- value, left and right...