Haskell non-exhaustive patterns in function with `

2019-02-28 01:12发布

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条回答
Emotional °昔
2楼-- · 2019-02-28 01:48

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.

查看更多
登录 后发表回答