Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun is_older( (yr1 : int, mth1 : int, day1 : int), (yr2 : int, mth2 : int, day2 : int)) =
- let
- val year = yr1 - yr2;
- val month = mth1 - mth2;
- val days = day1 - day2;
- in
- if year = 0
- then
- if month = 0
- then
- if days = 0
- then false
- else days < 0
- else month < 0
- else year < 0
- end
- fun number_in_month(dates : (int * int * int) list, month: int) =
- if null dates
- then 0
- else
- let
- val date = hd dates
- val date_month = #2 date
- in
- if date_month = month
- then 1 + number_in_month(tl dates, month)
- else 0 + number_in_month(tl dates, month)
- end
- fun number_in_months(dates: (int * int * int) list, months: int list) =
- if null months
- then 0
- else
- if null dates
- then 0
- else number_in_month(dates, (hd months)) + number_in_months(dates, (tl months))
- fun dates_in_month(dates : (int * int * int) list, month : int) =
- if null dates
- then []
- else
- let
- val date = hd dates
- val date_month = #2 date
- in
- if date_month = month
- then date :: dates_in_month(tl dates, month)
- else dates_in_month(tl dates, month)
- end
- fun dates_in_months(dates: (int * int * int) list, months: int list) =
- if null months
- then []
- else
- if null dates
- then []
- else
- let
- val new_dates = dates_in_month(dates, (hd months))
- in
- if null new_dates
- then dates_in_months(dates, (tl months))
- else (hd new_dates) :: dates_in_months(dates, (tl months))
- end
- fun get_nth (strings : string list, nth: int) =
- if nth = 1
- then hd strings
- else get_nth(tl strings, nth -1)
- fun date_to_string(date: (int * int * int)) =
- let
- val months = [ "January", "February", "March", "April", "May",
- "June", "July", "August", "September", "October",
- "November", "December"
- ];
- val month = get_nth(months, #2 date)
- in
- month ^ " " ^ Int.toString(#3 date) ^ ", " ^ Int.toString(#1 date)
- end
- fun number_before_reaching_sum(sum: int, positives: int list) =
- let
- val first = hd positives;
- val new_sum = sum - first;
- in
- if new_sum <= 0
- then 0
- else 1 + number_before_reaching_sum(new_sum, (tl positives))
- end
- fun what_month(days: int) =
- let
- val days_in_month = [31, 28, 31, 30, 31,
- 30, 31, 31, 30, 31,
- 30, 31
- ];
- in
- number_before_reaching_sum(days, days_in_month) + 1
- end
- fun month_range(day1: int, day2: int) =
- if day2 < day1
- then []
- else what_month(day1) :: month_range(day1 + 1, day2)
- fun oldest(dates: (int * int * int) list) =
- if null dates
- then NONE
- else
- if null (tl dates)
- then SOME (hd dates)
- else
- let
- fun oldest_with_at_least_two() =
- let
- val first_date = hd dates
- val second_date = hd (tl dates)
- val first_is_older = is_older(first_date, second_date);
- in
- if first_is_older
- then first_date
- else second_date
- end
- in
- oldest(oldest_with_at_least_two() :: (tl (tl dates)))
- end
- fun remove_duplicated(numbers: int list) =
- if null numbers
- then []
- else
- if null (tl numbers)
- then numbers
- else
- let
- val first = hd numbers;
- fun is_in(numbers: int list) =
- if null numbers
- then false
- else
- if first = hd numbers
- then true
- else is_in(tl numbers);
- in
- if is_in (tl numbers)
- then remove_duplicated(tl numbers)
- else first :: remove_duplicated(tl numbers)
- end;
- fun number_in_months_challenge(dates: (int * int * int) list, months: int list) =
- let
- val months = remove_duplicated(months)
- in
- number_in_months(dates, months)
- end
- fun dates_in_months_challenge(dates: (int * int * int) list, months: int list) =
- let
- val months = remove_duplicated(months)
- in
- dates_in_months(dates, months)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement