Haskell 二叉树的插入函数
Haskell Insert function for Binary Trees
我正在尝试创建一个名为 "insertm" 的函数,该函数应该将键和值插入二进制文件 tree.If 键已经存在,应该 return "nothing".如果不是,它应该根据它的值将键和值插入到树中。我能够完成其中的大部分工作,但是我遇到了一个错误,我不确定如何修复。
这是一个例子:
TestQ4> insertm 25 "vw" t5
Just (10:"ghi")<$,(30:"def")<(20:"abc")<$,(25:"vw")>,$>>
TestQ4> insertm 20 "vw" t5
Nothing
这是我的代码:
data BinaryTree a b = Leaf | Node a b (BinaryTree a b) (BinaryTree a b)
insertm :: (Ord a, Show a, Show b) =>
a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = Just (Node val key (insertm x y left) right)
| otherwise = Just (Node val key left (insertm x y right))
这是我得到的错误:
* Couldn't match expected type `BinaryTree a b'
with actual type `Maybe (BinaryTree a b)'
* In the fourth argument of `Node', namely `(insertm x y right)'
In the first argument of `Just', namely
`(Node val key left (insertm x y right))'
In the expression: Just (Node val key left (insertm x y right))
* Relevant bindings include
right :: BinaryTree a b (bound at TestQ4.hs:101:32)
left :: BinaryTree a b (bound at TestQ4.hs:101:27)
key :: b (bound at TestQ4.hs:101:23)
val :: a (bound at TestQ4.hs:101:19)
y :: b (bound at TestQ4.hs:101:11)
x :: a (bound at TestQ4.hs:101:9)
(Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-
relevant-binds)
| x < val = Just (Node val key (insertm x y left) right)
^^^^^^^^^^^^^^^^
我的其他情况也出现错误。所以我有点卡住了任何帮助将不胜感激。
问题是 (insertm x y left)
是一个 Maybe (BinaryTree a b)
in:
| x < val = Just (Node val key (insertm x y left) right)
不是BinaryTree a b
,因此你不能只用Maybe (BinaryTree a b)
作为子树构造这样的BinaryTree
。
但是您可以 "unpack" 值,然后使用它,例如:
insertm :: (Ord a, Show a, Show b) => a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = case insertm x y left of
Just l -> Just (Node val key l right)
Nothing -> Nothing
| otherwise = case insertm x y right of
Just r -> Just (Node val key left r)
Nothing -> Nothing
上面的模式很流行,我们可以在这里使用fmap :: Functor f => (a -> b) -> f a -> f b
,将Just x
中的x
映射到Just (f x)
并映射Nothing
在 Nothing
:
insertm :: (Ord a, Show a, Show b) => a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = fmap (flip (Node val key) right) (insertm x y left)
| otherwise = fmap (Node val key left) (insertm x y right)
或喜欢说:
insertm :: (Ord a, Show a, Show b) => a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = Node val key <$> insertm x y left <*> pure right
| otherwise = Node val key left <$> insertm x y right
(<$>)
是等价于 fmap
的函数,而 (<*>) :: f (a -> b) -> f a -> f b
是一个取一个 Maybe (BinaryTree a b -> BinaryTree a b)
并应用函数 [=29] 的函数=] 包裹在 Just
中,值 x
包裹在右边 Just
和 returns Just (f x)
鉴于两者因此 Just
s ,如果两者之一是 Nothing
(或两者),则它将 return Nothing
.
我正在尝试创建一个名为 "insertm" 的函数,该函数应该将键和值插入二进制文件 tree.If 键已经存在,应该 return "nothing".如果不是,它应该根据它的值将键和值插入到树中。我能够完成其中的大部分工作,但是我遇到了一个错误,我不确定如何修复。
这是一个例子:
TestQ4> insertm 25 "vw" t5
Just (10:"ghi")<$,(30:"def")<(20:"abc")<$,(25:"vw")>,$>>
TestQ4> insertm 20 "vw" t5
Nothing
这是我的代码:
data BinaryTree a b = Leaf | Node a b (BinaryTree a b) (BinaryTree a b)
insertm :: (Ord a, Show a, Show b) =>
a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = Just (Node val key (insertm x y left) right)
| otherwise = Just (Node val key left (insertm x y right))
这是我得到的错误:
* Couldn't match expected type `BinaryTree a b'
with actual type `Maybe (BinaryTree a b)'
* In the fourth argument of `Node', namely `(insertm x y right)'
In the first argument of `Just', namely
`(Node val key left (insertm x y right))'
In the expression: Just (Node val key left (insertm x y right))
* Relevant bindings include
right :: BinaryTree a b (bound at TestQ4.hs:101:32)
left :: BinaryTree a b (bound at TestQ4.hs:101:27)
key :: b (bound at TestQ4.hs:101:23)
val :: a (bound at TestQ4.hs:101:19)
y :: b (bound at TestQ4.hs:101:11)
x :: a (bound at TestQ4.hs:101:9)
(Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-
relevant-binds)
| x < val = Just (Node val key (insertm x y left) right)
^^^^^^^^^^^^^^^^
我的其他情况也出现错误。所以我有点卡住了任何帮助将不胜感激。
问题是 (insertm x y left)
是一个 Maybe (BinaryTree a b)
in:
| x < val = Just (Node val key (insertm x y left) right)
不是BinaryTree a b
,因此你不能只用Maybe (BinaryTree a b)
作为子树构造这样的BinaryTree
。
但是您可以 "unpack" 值,然后使用它,例如:
insertm :: (Ord a, Show a, Show b) => a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = case insertm x y left of
Just l -> Just (Node val key l right)
Nothing -> Nothing
| otherwise = case insertm x y right of
Just r -> Just (Node val key left r)
Nothing -> Nothing
上面的模式很流行,我们可以在这里使用fmap :: Functor f => (a -> b) -> f a -> f b
,将Just x
中的x
映射到Just (f x)
并映射Nothing
在 Nothing
:
insertm :: (Ord a, Show a, Show b) => a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = fmap (flip (Node val key) right) (insertm x y left)
| otherwise = fmap (Node val key left) (insertm x y right)
或喜欢
insertm :: (Ord a, Show a, Show b) => a -> b -> BinaryTree a b -> Maybe (BinaryTree a b)
insertm val key Leaf = Just (Node val key Leaf Leaf)
insertm x y (Node val key left right)
| x == val = Nothing
| x < val = Node val key <$> insertm x y left <*> pure right
| otherwise = Node val key left <$> insertm x y right
(<$>)
是等价于 fmap
的函数,而 (<*>) :: f (a -> b) -> f a -> f b
是一个取一个 Maybe (BinaryTree a b -> BinaryTree a b)
并应用函数 [=29] 的函数=] 包裹在 Just
中,值 x
包裹在右边 Just
和 returns Just (f x)
鉴于两者因此 Just
s ,如果两者之一是 Nothing
(或两者),则它将 return Nothing
.