user3763118
user3763118

Reputation: 31

function doesn't recognize pattern-matching

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.

Upvotes: 1

Views: 125

Answers (1)

Mark Whitfield
Mark Whitfield

Reputation: 2528

[_] 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.

Upvotes: 7

Related Questions