1
votes

My function paralellenCheck throws an exception everytime I call it complaining about Non-exhaustive patterns

paralellenCheck is defined as follows:

paralellenCheck :: (Eq a, Num a ) => [a] -> [a] -> Bool
paralellenCheck [] [] = True
paralellenCheck [_] [] = True
paralellenCheck [] [_] = True
paralellenCheck (x:xs) (y:ys)
  | intervall `elem`  [0,5,7,12]  = False
  | not $ paralellenCheck (x:xs) ys  = False 
  | not $ paralellenCheck xs (y:ys)  = False
  | otherwise  = True
  where intervall = abs (x - y)

running the function in GHCI with -Wall applied just returns

<interactive>:10:1:
Warning: Defaulting the following constraint(s) to type `Integer'
           (Eq a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
           (Num a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
In the expression: paralellenCheck [] [2, 3, 4, 5]
In an equation for `it': it = paralellenCheck [] [2, 3, 4, ....]

<interactive>:10:1:
Warning: Defaulting the following constraint(s) to type `Integer'
           (Eq a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
           (Num a0)
             arising from a use of `paralellenCheck' at <interactive>:10:1-15
In the expression: paralellenCheck [] [2, 3, 4, 5]
In an equation for `it': it = paralellenCheck [] [2, 3, 4, ....]
*** Exception: haskelltest.hs:(6,1)-(14,31): Non-exhaustive patterns in function paralellenCheck

I am rather new to haskell and rather confused. I thought the pattern-matching with [_] [] and [] [_] should resolve this issue.

1
Try replacing your [_] with just _. Also, you might want to supply your definitions for (x:xs) (y:ys) first, and then supply a _ _ = True pattern afterwards. - C. Quilley
wouldn't this destroy the recursion though? every new recursion would start a new one before having the chance to cancel by _ _ - user3763118

1 Answers

7
votes

[_] in a pattern means "any list of exactly one element". You seem to have intended "any list at all". Right now, [] (a:b:[]), for example, is not matched. Use _ for any list.