Consider a function disjoint :: (Eq a, Ord a) => [a] -> [a] -> Bool
which checks if there exists a common element between two sorted lists (and if it does, it returns False
since they wouldn't be disjointed).
disjoint :: (Eq a, Ord a) => [a] -> [a] -> Bool
disjoint [] _ = True
disjoint _ [] = True
disjoint (x:xs) (y:ys)
| x == y = False
| x < y = disjoint xs (y:ys)
| x > y = disjoint (x:xs) ys
In my IDE vscode
, it yields the following warning:
Pattern match(es) are non-exhaustive
In an equation for ‘disjoint’: Patterns not matched: (_:_) (_:_)compile(-Wincomplete-patterns)
Writing it in a different yet similar way, by using the otherwise
clause, will yield no errors:
disjoint :: (Eq a, Ord a) => [a] -> [a] -> Bool
disjoint [] _ = True
disjoint _ [] = True
disjoint (x:xs) (y:ys)
| x < y = disjoint xs (y:ys)
| x > y = disjoint (x:xs) ys
| otherwise = False
What "pattern" does the first code block "miss"? In other words, at what parameters will the first block of code have problems with?