2014-11-23 42 views
-1

我試圖瞭解樹木中的標籤,但我無法理解。這是我Datentyp的樹:地圖和郵政編碼哈斯克爾的樹

data Tree = Node (Label -> Label) Label [Tree] 
type Label = Int 

我無法理解真正的,之間有什麼(標籤 - >標籤)的區別和標籤? 我怎樣才能從這些功能得到正確的結果?

tcm :: (Label -> Bool) -> (Label -> Label) -> Tree -> Tree 
    tzp :: (Label -> Label -> Label) -> Tree -> Tree -> Tree 
    tmax :: Label -> Tree -> (Label -> Label) 

繼承人的例子樹和不同datentyp樹木和類似的功能類似的例子:

t1 = Null 
t2 = Tree 2 (Tree 3 Null Null) (Tree 5 Null Null) 
t3 = Tree 2 (Tree 3 (Tree 5 Null Null) Null) (Tree 7 Null Null) 

data Tree = Null | Tree Label Tree Tree deriving (Eq,Show) 
type Label = Integer 

但在我的例子樹心不是空還是什麼?

tcm :: (Label -> Label) -> Tree -> Tree 
tzp :: (Label -> Label -> Label) -> Tree -> Tree -> Tree 

tcm (+1) t1 == Null 
tcm (+1) t2 == Tree 3 (Tree 4 Null Null) (Tree 6 Null Null) 
tcm (+1) t3 == Tree 3 (Tree 4 (Tree 6 Null Null) Null) (Tree 8 Null Null) 

tzp (+) t1 t2 == Null 
tzp (+) t2 t3 == Tree 4 (Tree 6 Null Null) (Tree 12 Null Null) 

有人可以幫我用這些函數和樹嗎?

回答

1

(標籤 - >標籤)和標籤本身之間的區別在於前者是一個函數,在給定標籤的情況下,該函數會生成一個新標籤。

至於你的下一個問題:在第一個Tree數據類型中,樹確實不能爲Null,因爲你的數據類型定義不允許這樣做。

除此之外,你已經用binary-tree標籤標記了你的問題,但是你想澄清的數據類型不是二叉樹的數據類型,因爲列表的長度可能不同。