functional programming - how to get an int list of months by giving two days of a year? -
write function named month_range takes 2 days of year named day_one , day_two (e.g. 65, 128, assuming year has 365 days) input , return int list of months. size of int list must day_two - day_one + 1;
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) , february(1,2,..,5)
i wrote code 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) 1 else 1 + aux(sum, (hd numbers + hd numbers_tail)::(tl numbers_tail)) end in aux(day, month_days) end in if (day1>day2) [] else what_month(day1) @ what_month(day2) end
well per previous question, have function what_month
, return month number of given day of year.
you pretty simple iterate day_one
through day_two
, calling what_month
function each time.
now putting in resulting list, done concatenating result of what_month
recursive call function index incremented one, concatenating empty list when index reach day_two
.
Comments
Post a Comment