我正在研究一个检查元素是否是二叉树的一部分的函数.我为我的树定义了一个类型,称为Tree
函数以获取根元素和左右子树,以及一个函数,isElement
用于检查值是否在我的树中.不幸的是,该函数仅适用于根元素.
以下示例说明了从isElement
函数中获得的错误结果:
*Main>let tree = Node 1 Empty (Node 2 Empty (Node 3 Empty Empty))
*Main> isElement tree 2
False
*Main> isElement tree 3
False
*Main> isElement tree 1
True
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
data Tree a = Node a (Tree a) (Tree a)
|Empty
deriving (Show)
nodeValue :: Tree t -> t
nodeValue (Node x _ _) = x
rightTree :: Tree t -> Tree t
rightTree (Node _ _ x) = x
leftTree :: Tree …
Run Code Online (Sandbox Code Playgroud) 我有以下haskell语句的问题:
insertSort3 xs =
let sort3 [] ys = ys
sort3 (x:xs) ys = sort3 xs (insert x ys)
in sort3 xs []
Run Code Online (Sandbox Code Playgroud)
我的编译器说:在输入'='上解析错误(错误发生在第三行).