SML错误未绑定变量

时间:2013-10-16 08:03:05

标签: sml

我正在尝试学习SML,我正在尝试实现两个功能。第一个函数工作正常,但是当我添加第二个函数时,它给了我一个运行时错误:

stdIn:1.2-1.17 Error: unbound variable or constructor: number_in_month

调用函数number_in_month时会发生这种情况。我的代码是:

 fun is_older(d1 :int*int*int,d2 :int*int*int) =
  (#1 d1) < (#1 d2) andalso (#2 d1) < (#2 d2) andalso (#3 d1) < (#3 d2)


fun number_in_month(da :(int * int * int) list ,mo : int) =
    if da = []
    then 0
    else if (#2(hd da)) = mo
     then 1 + number_in_month((tl da),mo)
    else 0 + number_in_month((tl da),mo)

1 个答案:

答案 0 :(得分:1)

您的问题听起来与其他问题非常相似:SML list iteration(8个月前),recursion in SML(9个月前)和Count elements in a list(8个月前),所以你肯定不要因提出创造性问题而获得积分。

上面的一些问题已经广泛得到了解答。看看他们。

这是以更好的方式重写的代码:

(* Find better variable names than x, y and z. *)
fun is_older ((x1,y1,z1), (x2,y2,z2)) =
    x1 < x2 andalso y1 < y2 andalso z1 < z2

fun number_in_month ([], mo) = 0
  | number_in_month ((x,y,z)::rest, mo) =
    if y = mo then 1 + number_in_month(rest, mo)
              else 0 + number_in_month(rest, mo)