2012-12-02 92 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