number_in_month exercise (Count elements in a list)

前端 未结 2 1791
情话喂你
情话喂你 2021-01-23 19:09

I have been trying to count elements in a list of integer 3-tuples, that equals a given integer using SML, but it\'s not working. Can anyone help me figure out what\'s wrong wit

2条回答
  •  不要未来只要你来
    2021-01-23 19:48

    fun number_in_month (L : (int*int*int) list, m : int) =
    if L = nil
    then 0
    else
        (if #2 (hd L) = m then 1 else 0) + number_in_month (tl L,m);
    

    TESTING:

    number_in_month ([] , 2);
    number_in_month ([(1,2,3)] , 2);
    number_in_month ([(1,2,3),(2,2,2)] , 2);
    number_in_month ([(1,2,3),(2,2,2),(19,11,29)] , 2);
    number_in_month ([(1,2,3),(2,2,2),(19,11,29),(10,28,19)] , 2);
    number_in_month ([(1,2,3),(2,2,2),(19,11,29),(10,2,19)] , 2);
    number_in_month ([(1,2,3),(2,2,2),(19,11,29),(10,28,19)] , 2);
    number_in_month ([(1,2,3),(2,2,2),(19,11,29),(10,28,19)] , 2);
    number_in_month ([(1,2,3),(2,2,2),(19,11,29),(10,28,19),(16,2,7)] , 2);
    

    Reference:

    http://www.cs.sunysb.edu/~leo/CSE215/smllistexamples.txt

    http://www.standardml.org/Basis/list.html

提交回复
热议问题