Skip to content

Instantly share code, notes, and snippets.

@arjkb
Created October 8, 2017 01:03
Show Gist options
  • Save arjkb/cc1fb94c1d5a329a93fa05152c1bf7b9 to your computer and use it in GitHub Desktop.
Save arjkb/cc1fb94c1d5a329a93fa05152c1bf7b9 to your computer and use it in GitHub Desktop.
A simple binary-tree implementation in haskell.
data Tree a = Leaf a
| Node (Tree a) (Tree a)
deriving (Show, Eq)
treeMap :: (a -> b) -> (Tree a) -> (Tree b)
treeMap f (Leaf a) = Leaf (f a)
treeMap f (Node t1 t2) = Node (treeMap f t1) (treeMap f t2)
treeFold :: (b -> b -> b) -> (a -> b) -> (Tree a) -> b
treeFold fnode fleaf (Leaf n) = fleaf n
treeFold fnode fleaf (Node t1 t2) = fnode (treeFold fnode fleaf t1) (treeFold fnode fleaf t2)
@tanujasudhakar
Copy link

Hi, thank you very much for the explanation, but I am running into the following error, help.
haskell_tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment