Haskell非穷尽模式在功能上与“否则” [英] Haskell non-exhaustive patterns in function with `otherwise`
本文介绍了Haskell非穷尽模式在功能上与“否则”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下功能:
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.
推荐答案
由于 l'(@:ys)
中的 l @(x:xs)
您缺少此案组合_ _ [[]
。
后卫 null l
始终为 False
。
这篇关于Haskell非穷尽模式在功能上与“否则”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文