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 to do it...

  1. Create a new Haskell project binary-tree-functor using the simple Stack template:
        stack new binary-tree-functor simple
  1. Open src/Main.hs. This is the file that will be used for our purposes.
  2. After adding module definition for Main, add the following import:
        module Main where        import Data.Functor
  1. 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 trees.        data Tree a = Leaf            | Node (Tree a) a (Tree a)             deriving (Show, Eq)
        -- Create a tree given a value, left tree and a right tree        node :: Tree a -> a -> Tree a -> Tree a        node l x r = Node l x r
       -- Induct a value into a new tree (node with empty left and         right trees) singleton :: a ...

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