Rob
Rob

Reputation: 45

Non-exhaustive patterns in haskell, created data type

I'm getting a Non-exhaustive pattern in my code on the 2nd line here , i think its intrusive as i need it for my recursion to stop at some point. It says patterns not matched : [ ] _

allergyFree :: [Ingredient] -> [Cupcake] -> [Cupcake]
allergyFree (y:ys) [] = []
allergyFree (y:ys) (c:cs) = if(not(igPresent (y:ys) recipeN)) then ([c] ++ (allergyFree (y:ys) cs))
                            else (allergyFree (y:ys) cs)

where
CC (P priceN) recipeN = c
igPresent :: [Ingredient] -> [Ingredient] -> Bool
igPresent [] recipeN = False
igPresent (y:ys) recipeN = if (y `elem` recipeN) then True
                           else igPresent ys recipeN

Any help would be appreciated !

P.s. i'm quite new to haskell

Upvotes: 2

Views: 186

Answers (2)

developer_hatch
developer_hatch

Reputation: 16224

When you do pattern matching over two list, you must add the patter of the empty list on both cases (if you want your function to be total):

allergyFree :: [Ingredient] -> [Cupcake] -> [Cupcake]
allergyFree _ [] = [] -- second list is empty, I don't care about the first
allergyFree [] _ = [] -- first list is empty, I don't care about the second
allergyFree (y:ys) (c:cs) = -- logic when both has elements

Upvotes: 2

Robin Zigmond
Robin Zigmond

Reputation: 18249

(y:ys) only matches non-empty lists - it refers to a list with first element y (and ys refers to the rest of the list). Since both of your patterns have (y:ys) for the first argument, the function will fail with this error if you ever try to pass it an empty list in that position.

I've only glanced at your code briefly, but since you don't use the y or ys variables individually anywhere in the function definition, this should work if you replace the (y:ys) pattern with a single name, say x, and then use x anywhere you have (y:ys) in your current definition.

Upvotes: 2

Related Questions