number_in_month exercise (SML error unbound variable)

前端 未结 1 961
花落未央
花落未央 2021-01-28 01:38

I am trying to learn SML, and I am trying to implement two functions. The first function works fine, but when I added the second function it gives me an run-time error:

1条回答
  •  后悔当初
    2021-01-28 01:54

    Your question sounds surprisingly much like these other questions: SML list iteration (8 months ago), recursion in SML (9 months ago), and Count elements in a list (8 months ago), so you certainly don't get points for asking a creative question.

    Some of the questions above have been answered extensively. Look at them.

    Here is your code rewritten in a better style:

    (* 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)
    

    0 讨论(0)
提交回复
热议问题