Haskell non-exhaustive patterns in function with `otherwise`

2.2k Views Asked by At

I am using the following function:

combinations :: Int -> [a] -> [[a]]
combinations k xs = combinations' (length xs) k xs
  where combinations' n k' l@(y:ys)
          | k' == 0   = [[]]
          | k' >= n   = [l]
          | null l    = []
          | otherwise = Prelude.map (y :) (combinations' (n - 1) (k' - 1) ys) ++ combinations' (n - 1) k' ys 

It works just fine for just about any example i can come up with, but when i call it from other functions in my larger project, for some cases I get an exception:

Exception: projekt.hs:(34,9)-(38,108): Non-exhaustive patterns in function combinations'

Is there something wrong with the above definition? Is it missing some case? I thought otherwise handles anything that doesn't fall into previous cases.

1

There are 1 best solutions below

0
On BEST ANSWER

Because of l@(x:xs) in combinations' n k' l@(y:ys) you're missing the case combinations _ _ [].

The guard null l will always be False.