我不知道为什么我的函数不起作用。我已经浏览了所有关于非详尽功能的帖子,但是我的功能尽可能地实现了所有可能的选项。Haskell:非详尽模式 - 检查列表是否正在升序
ascending :: [Int] -> Bool
ascending [] = error "Empty list given"
ascending [x] = True
ascending [x,y] | y>=x = True
| x<y = False
ascending (x:y:xs) | y>=x = (ascending (y:xs))
| x<y = False
结果:
*Main> ascending []
*** Exception: Empty list given
*Main> ascending [1]
True
*Main> ascending [1, 2]
True
*Main> ascending [2, 1]
*** Exception: test01.hs:(51,1)-(56,55): Non-exhaustive patterns in function ascending
它为一对,但如果对不上升。当我遵循我的代码时,它应该只是返回False。
@jubobs是没有被[x,y]和[x]覆盖?如果有0,1,2,3+条款,那么每件事都有前进的道路。此外,为什么只有非升序(错误)列表失败? – deadfire19 2015-02-08 00:12:25
仔细查看比较结果。使用'[2,1]'调用哪个分支时应该关闭? – 2015-02-08 00:12:40
@David我会认为它会下降[x,y] x = 2和y = 1 .....然后点击....我觉得自己像一个白痴。那么现在我知道'非穷举'也包括守卫。谢谢! – deadfire19 2015-02-08 00:18:10