I have the following code which defines a function called merge
:
merge :: Ord a => [a] -> [a] -> [a]
merge [] [] = []
merge [] ys = ys
merge xs [] = xs
merge x y | head x <= head y = x ++ y
| head x > head y = y ++ x
merge x (y:ys) | head x <= y = x ++ merge [] (y:ys)
| head x > y = y: merge x ys
merge (x:xs) y | x <= head y = x: merge xs y
| x > head y = y ++ merge (x:xs) []
merge (x:xs) (y:ys) | x <= y = x : merge xs (y:ys)
| x > y = y : merge (x:xs) ys
This gives me the following warning:
Patterns not matched:
[_] [_]
[_] (_:_:_)
(_:_:_) [_]
(_:_:_) (_:_:_)
Looking at my code it's not clear to me what patterns I've missed. What extra patterns should I define to get rid of this warning?
As an aside, this is the same function but implemented with conditionals, this results in no warning. What is the difference between the pattern matched and guarded functions and the conditional function?
merge (x:xs) (y:ys) = if x <= y then x : merge xs (y:ys) else y : merge (x:xs) ys