模式匹配不正确
我有点新手Haskell,
data Tree a = Leaf | Branch (Tree a) a (Tree a)
deriving (Show, Eq)
insert :: (Ord a, Eq a) => a -> Tree a -> Tree a
insert x Leaf = Branch Leaf x Leaf
insert x (Branch l v r)
| x <= v = Branch (insert x l) v r
| x > v = Branch l v (insert x r)
代码会发出以下警告(但编译):
app\Main.hs:10:1: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for `insert':
Patterns not matched:
_ (Branch Leaf _ Leaf)
_ (Branch Leaf _ (Branch _ _ _))
_ (Branch (Branch _ _ _) _ Leaf)
_ (Branch (Branch _ _ _) _ (Branch _ _ _))
|
10 | insert x Leaf = Branch Leaf x Leaf
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
从技术上讲 x&lt; = v
和 x&gt; V
应该涵盖所有可能的替代方案,但是我有此警告。如果我在第二种情况下使用,我实际上是修复的,但是它不应该这样起作用吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ord
实例应该定义总订单,但是Haskell无法执行这一要求,也无法检测到ord od> ord
实例确实是总订单。使用
否则
不起作用,因为它是true
(按字面定义,它是x&lt; = v
是false,但因为它是
true
,无论x&lt; = v
是TRUE还是FALSE, 。知道&lt; =
和&gt;
每个 can 返回true
不保证其中一个 。An
Ord
instance should define a total order, but Haskell is unable to enforce that as a requirement nor is it able to detect if anOrd
instance is, indeed, a total order.Using
otherwise
works not because it isTrue
(which it is, by literal definition) whenx <= v
isFalse
, but because it isTrue
regardless of whetherx <= v
is true or false. Knowing that<=
and>
each can returnTrue
does not guarantee that one of them will.