【问题标题】:Warning: match nonexhaustive - handling a specific case (SML)警告:匹配非详尽 - 处理特定情况 (SML)
【发布时间】:2018-05-18 17:20:13
【问题描述】:

考虑以下 SML 列表示例:[[("foo",~10.0)],[("goo",~8.0)]]

我将链接编写一个函数,该函数将删除主括号,这意味着输出将是:

[("foo", ~10.0), ("goo, ~8.0)]

我写的函数:

fun inner_list [[]] = [] | inner_list [] = [] 
|  inner_list ((((x:(string*real))::xt)::xs)) = x :: inner_list xs;

它适用于大多数情况,但我知道我没有检查其中一种情况。我认为这种情况是:

[[],("foo", ~10.0)]

我知道我没有处理其中一种情况,因为编译器会发出警报:

stdIn:1.6-2.68 Warning: match nonexhaustive
      nil :: nil => ...
      nil => ...
      (x :: xt) :: xs => ...

我阅读了与Warning: match nonexhaustive 警告相关的其他文章,但我不明白如何用我的程序解决它。

其他情况如何处理?

编辑 我知道我的列表只包含一个列表。这就是为什么我不使用xt

【问题讨论】:

  • 你怎么对xt什么都不做?
  • @sepp2k 因为我知道列表中只有一个列表。生病编辑我的描述
  • 您需要决定如何处理这种情况。你应该失败,或者只是把那个元素排除在外,还是别的什么?
  • [[],("foo", ~10.0)] 不是有效大小写,因为该表达式的类型错误。
  • @molbdnilo 我希望它返回没有nil 列表的子列表。 [[],("foo", ~10.0)] 将返回 [("foo", ~10.0)]

标签: sml


【解决方案1】:

内置的List.concat怎么样?

List.concat [[("foo",~10.0)], [("goo",~8.0)]] =
    [("foo",~10.0), ("goo",~8.0)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多