write a function named month_range that takes two days of a year named day_one and day_two (e.g. 65, 128, assuming the year has 365 days) as input and return an int list of its months.
the size of this int list must be day_two - day_one + 1;
be aware, if day_one>day_two, size of list = 0
example : month_range(25,36) should return [1,1,1,1,1,1,1,2,2,2,2,2]
January(25,26,27,..,31) and February(1,2,..,5)
I wrote the code but it doesn't work :
fun month_range (day1:int,day2:int) =
let
val month_days= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
fun what_month(day :int) =
let
fun aux(sum :int, numbers: int list) =
let
val numbers_tail = tl numbers
in
if sum <= (hd numbers)
then 1
else
1 + aux(sum, (hd numbers + hd numbers_tail)::(tl numbers_tail))
end
in
aux(day, month_days)
end
in
if (day1>day2)
then []
else what_month(day1) @ what_month(day2)
end
Well as per your previous question, you have a function what_month
, which will return the month number of a given day of the year.
You could pretty simple iterate from day_one
through day_two
, calling your what_month
function each time.
Now putting it all in a resulting list, could be done by concatenating the result of what_month
with a recursive call to your function with the index incremented by one, concatenating with the empty list when your index reach day_two
.