Haskell non-exhaustive patterns in function with `otherwise`

佐手、 提交于 2019-12-02 09:33:09

问题


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:


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.



来源:https://stackoverflow.com/questions/30491133/haskell-non-exhaustive-patterns-in-function-with-otherwise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!