这是我的代码:
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
答案 0 :(得分: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