Reputation: 325
I have the following function:
union :: (Eq a) => Set1 a -> Set1 a -> Set1 a
union (MakeSet (x:xs)) (MakeSet (y:ys)) = if (member (MakeSet (y:ys)) x) && (not (isEmpty (MakeSet (x:xs))))
then union (MakeSet xs) (MakeSet (y:ys))
else if (not (member (MakeSet (y:ys)) x)) && (not (isEmpty (MakeSet (x:xs))))
then union (MakeSet xs) (insert x (MakeSet (y:ys)))
else MakeSet (y:ys)
It compiles, but when I execute it with parameters I get that the function has non-exhaustive patterns.
First of all, why does it think I am using patterns? I'm only using conditional statements, and I have an else statement which should catch everything not caught by the if then and else if then statements. Can someone tell me what is happening here?
Upvotes: 0
Views: 124
Reputation: 7266
Your code
union (MakeSet (x:xs)) (MakeSet (y:ys)) = ...
means the same thing as
union = \a b -> case (a, b) of
(MakeSet (x : xs), MakeSet (y : ys)) -> ...
so yes, your code is already using pattern matching. See translation for function bindings in the Haskell 2010 report.
The problem is (as noted in amalloy's answer) that you are matching on MakeSet (... : ...)
but not on MakeSet []
, so you're missing some case. To fix this, add more equations to match on the missing cases.
Upvotes: 1
Reputation: 91857
(MakeSet (x:xs))
is a pattern, and it is not exhaustive: it doesn't match MakeSet []
.
Upvotes: 7