【问题标题】:Warning appear after I solve a recursion question in Haskell在 Haskell 中解决递归问题后出现警告
【发布时间】:2021-12-13 09:55:52
【问题描述】:

我正在尝试执行下面的代码问题,但我收到了这种类型的警告。我不知道发生了什么,因为我可以正确输出答案。以下是我的代码和警告:

continuous :: [Integer] -> Bool
continuous list = case list of 
            [] -> True
            [x,y] 
                 | abs (x-y) <= 1 -> True 
                 | otherwise -> False
            x:y:xs 
                | abs(x-y) <= 1 -> continuous (y:xs)
                | otherwise -> False 
Lists.hs:43:19: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In a case alternative: Patterns not matched: [_]
   |
43 | continuous list = case list of 
   |                   ^^^^^^^^^^^^^...

【问题讨论】:

    标签: list haskell recursion


    【解决方案1】:

    警告指出,您的模式匹配并不详尽,因为您缺少包含单个元素的列表的情况,例如 [x]

    另外,如果为单个元素添加 case,则不再需要 case [x,y],因为它也由 case x:y:xs 处理,因为 xs 也可以表示一个空列表

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2020-06-30
      • 2020-03-26
      相关资源
      最近更新 更多