tomss
tomss

Reputation: 269

Instance show with Tree data structure

I am new at haskell and I want to instance Tree a with the class show.

  data Tree a = Null
               |Node (Tree a) a (Tree a)

  instance Show (Tree a) where
    show Null = ""
    show Node ((Tree l) (v) (Tree r)) = "|"--I don´t know how i can do this step

Thanks for your help.

Upvotes: 1

Views: 4602

Answers (1)

Abhinav Sarkar
Abhinav Sarkar

Reputation: 23802

Apply show recursively:

data Tree a = Null | Node (Tree a) a (Tree a)

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

Upvotes: 4

Related Questions