Iva*_*van 0 haskell pattern-matching
我对 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)
Run Code Online (Sandbox Code Playgroud)
该代码给出以下警告(但可以编译):
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
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
Run Code Online (Sandbox Code Playgroud)
从技术上讲x <= v,x > v应该涵盖所有可能的替代方案,但我收到此警告。如果我otherwise在第二种情况下使用,我实际上已经修复了,但是它不应该像这样工作吗?
一个Ord实例应该定义一个全序,但 Haskell 无法将其作为要求强制执行,也无法检测一个Ord实例是否确实是一个全序。
Prelude> data Foo = A | B deriving Eq
Prelude> instance Ord Foo where _ <= _ = False; _ > _ = False
Prelude> A <= B
False
Prelude> A > B
False
Run Code Online (Sandbox Code Playgroud)
使用otherwise之所以有效,并不是因为True当x <= vis时它就是(根据字面定义,它就是) False,而是因为True 无论是x <= vtrue 还是 false,它都是 。知道每个人都能回来<=并不能保证其中一个会回来。>True
| 归档时间: |
|
| 查看次数: |
147 次 |
| 最近记录: |