In merge'
and merge''
you assume the first argument to be of the form x:xs
, this excludes the empty list, and is causing the problems.
For example
head z@(x:xs) = x
will produce 1
as expected when calling head([1,2,3])
, but head([])
will throw a Non-exhaustive patterns in function head
exception because []
does not match x:xs
(the @
is not really important here).
In particular, this means that in merge'
the z == []
case can never be matched and also merge''
will throw an exception if you call merge [] [1,2,3]
for example.
Also note that in merge''
the case xs == []
is problematic. If, for example, we call merge'' [1] [2]
, then this case is matched because [1]
is 1:[]
. Then, just ys
, i.e., [2]
is returned and the x
, which is 1
, gets lost.