【问题标题】:nonexhaustive binding failure in sml codesml 代码中的非详尽绑定失败
【发布时间】:2017-04-07 11:03:55
【问题描述】:

我应该纠正一个函数 less(e, L) int * int list -> int list,它返回 L 中小于 e 的所有元素的列表。我是这样写的:

    fun less(_, nil) = nil
      | less(e, L) =
        let
           val x::xs = less (e, tl L)
        in
           if e > hd L then hd L::x::xs
           else nil @ x::xs
        end;

我遇到了绑定失败,肯定是在输入位上。我已经尝试了很多不同的thinfs,但我无法弄清楚为什么这是错误的。有人能解释一下吗?

【问题讨论】:

    标签: smlnj


    【解决方案1】:
    val x::xs = less (e, tl L)
    

    这与less (e, t1 L)的结果为空列表的情况不匹配。

    函数的正确实现是这样的:

    fun less (_, nil) = nil
      | less (y, x::xs) =
        let
           val xs' = less (y, xs)
        in
          if x < y then x::xs' else xs'
    

    【讨论】:

    • 非常感谢您的帮助。如果我可以问,lt(y, xs) 是什么?
    • 错字,应该是对less的递归调用
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多