0

I have previous asked, but no answer so I will try to rewrite the problem.

I have two lists. first list is dates (int * int * int) list and second list is only months. eg(1,2,3,4,5,6,7,8,9,10,11,12) int list

NOTE: assume any number in month list is not repeated and only 1-12.

I want to check how many dates has the same month as in the list of month. example: [(87,09,08),(67,08,17),(90,08,23)], [1,5,8] = 2

I know how to recursively compare a list with a number, but i cant figure out how to recursivly compare a list with a list...

fun number_in_months (dates :( int * int * int) list, months : int list)=
  if null dates
  then 0
  else if null months
  then 0
  else
      let 
      fun dm_notempty (dates : (int * int * int) list, months : int list)=
          if (null (tl dates) andalso null (tl months)) andalso (#2 (hd dates) <> hd months)
          then 0
          else if (null (tl dates) andalso null (tl months)) andalso (#2 (hd dates) = hd months)
          then 1
          else
          let val dates_tl = dm_notempty(tl dates, tl months)
          in
              if #2(hd dates) = hd months
              then dates_tl + 1
              else dates_tl + 0       
          end
      in
      dm_notempty(dates, months)
      end
sshine
  • 15,635
  • 1
  • 41
  • 66
pad
  • 21
  • 2

1 Answers1

0

Break the problem down into smaller problems.

For instance, imagine that you had a function number_in_month that counts the occurrences of a single month.

Then you could write this as

fun number_in_months (dates, []) = 0
  | number_in_months (dates, x::xs) =  number_in_month(dates, x) + number_in_months(dates, xs)

Now all that remains is that function, which is simpler to write because it only has one list argument, not two.
(Implementing it left as an exercise.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82