2012-12-02 96 views
1

我是哈斯克尔的新手,我想用类show来实例树a。树数据结构的实例显示

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 

感谢您的帮助。

+1

大括号是在错误的地方。尝试show(Node(Tree l)(v)(Tree r))= ...' –

+0

它给出一个错误ERROR file:。\ Tree.hs:6 - 未定义的数据构造函数“Tree” – tomss

+0

@tomss I comment on在Abhinav的回答下,简单地删除'树'。 –

回答

4

应用show递归:

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 ++ ")" 
+2

请注意,它应该是模式中的'(Node l v r)','Tree'是类型构造函数,而不是数值构造函数。 –

+0

感谢您的帮助。 – tomss