如何在Haskell中实现isBst函数 isBST::Ord a=>树a->Bool isBST Null=True isBST节点x t1 t2=isBST t1和isBST t2和测试t1 哪里 测试空值=真值 测试节点v_uf=f v x
更新: 在您的解决方案中,有几个问题: isBST Null=False。所有树都至少包含一个空节点。如果Null不是BST,则任何树都不能是BST。 isBST t1如何在Haskell中实现isBst函数 isBST::Ord a=>树a->Bool isBST Null=True isBST节点x t1 t2=isBST t1和isBST t2和测试t1 哪里 测试空值=真值 测试节点v_uf=f v x,haskell,Haskell,更新: 在您的解决方案中,有几个问题: isBST Null=False。所有树都至少包含一个空节点。如果Null不是BST,则任何树都不能是BST。 isBST t1
isBST :: Ord a => Tree a -> Bool
isBST Null = False
isBST (Node x t1 t2) = if (((isBST t1) < x) && ((isBST t2) > x)) then True else False
[Error]
<interactive>:3:42: error:
• Couldn't match expected type ‘Bool’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for:
isBST :: forall a. Ord a => Tree a -> Bool
at <interactive>:1:1-32
• In the second argument of ‘(<)’, namely ‘x’
In the first argument of ‘(&&)’, namely ‘((isBST t1) < x)’
In the expression: (((isBST t1) < x) && ((isBST t2) > x))
• Relevant bindings include
t2 :: Tree a (bound at <interactive>:3:18)
t1 :: Tree a (bound at <interactive>:3:15)
x :: a (bound at <interactive>:3:13)
isBST :: Tree a -> Bool (bound at <interactive>:2:1)