2013-05-15 47 views
0

這裏是我的代碼:大小的樹,而不是在範圍

data Binary_Tree a = Null 
         |Node {element :: a, left_tree, right_tree :: Binary_Tree a} 
    deriving (Show, Eq) 

--depth_of_tree 

dot :: Integral b => Binary_Tree a -> b 
dot Null = 0 
dot Node (a left right) = 1 + (dot Node (a left right)) + (dot Node (a left right)) 

然而,當我加載它在ghci中它,並輸入

dot Node (2 (Node 3 Null Null) Null) 

它出來一個錯誤:

<interactive>:13:1: 
    Not in scope: `dot' 
    Perhaps you meant `not' (imported from Prelude) 

任何人都喜歡告訴我我的代碼有什麼問題嗎?

感謝任何人誰可以很好我一些建議XD

回答

1

1)申報錯誤(代碼包含無限遞歸)。試試這個:

--depth_of_tree 
dot :: Integral b => Binary_Tree a -> b 
dot Null = 0 
dot (Node _ left right) = 1 + dot left + dot right 

2)帶圓括號的錯誤。試試這個

dot $ Node 2 (Node 3 Null Null) Null