number_in_month exercise

前端 未结 4 1986
悲哀的现实
悲哀的现实 2021-01-17 04:17

I am fresh on SML and doing a homework by that. \"Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns how many dates in the li

4条回答
  •  天命终不由人
    2021-01-17 04:36

    Your problem is endless recursion. Compiler can't get out of it because independ of result if..then..else you're running your function again Try this:

    fun number_in_month (dates: (int*int*int) list,month:int) = 
        if null dates
        then 0
        else if ((#2 (hd dates)) = month)
        then val flag=1 flag+number_in_month(tl dates, month) 
        else number_in_month((tl dates),month)
    

提交回复
热议问题