Toy example showing how to print an ADT (tree here) in haskell.

-- Toy example showing how to print an ADT.

data Tree a = Nil | Node a (Tree a) (Tree a)
leaf x = Node x Nil Nil

instance (Show a) => Show (Tree a) where
    show (Nil) = "Nil"
    show (Node a l r) = "(Node " ++ show a ++ " " ++ show l ++ " " ++ show r ++ ")" 

{- Define the following tree:
        a
       / \ 
      b   c
     / \
    d   e
   / \   \
  f   g   h
       \
         i
-}
t = (Node "a" (Node "b" (Node "d" (leaf "f") 
                                  (Node "g" Nil 
                                            (leaf "i"))) 
                        (Node "e" Nil 
                                  (leaf "h")))
              (leaf "c"))

main = putStrLn (show t)
view raw print_tree.md hosted with ❤ by GitHub

Comments