Advertisement
Guest User

Untitled

a guest
Sep 8th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun is_older( (yr1 : int, mth1 : int, day1 : int), (yr2 : int, mth2 : int, day2 : int)) =
  2.     let
  3.         val year = yr1 - yr2;
  4.         val month = mth1 - mth2;
  5.         val days = day1 - day2;
  6.     in
  7.         if year = 0
  8.         then
  9.             if month = 0
  10.             then
  11.                 if days = 0
  12.                 then false
  13.                 else days < 0
  14.             else month < 0
  15.         else year < 0
  16.     end
  17.  
  18. fun number_in_month(dates : (int * int * int) list, month: int) =
  19.     if null dates
  20.     then 0
  21.     else
  22.         let
  23.             val date = hd dates
  24.             val date_month = #2 date
  25.         in
  26.             if date_month = month
  27.             then 1 + number_in_month(tl dates, month)
  28.             else 0 + number_in_month(tl dates, month)
  29.         end
  30.  
  31. fun number_in_months(dates: (int * int * int) list, months: int list) =
  32.     if null months
  33.     then 0
  34.     else
  35.         if null dates
  36.         then 0
  37.         else number_in_month(dates, (hd months)) + number_in_months(dates, (tl months))
  38.  
  39.  
  40. fun dates_in_month(dates : (int * int * int) list, month : int) =
  41.     if null dates
  42.     then []
  43.     else
  44.         let
  45.             val date = hd dates
  46.             val date_month = #2 date
  47.         in
  48.             if date_month = month
  49.             then date :: dates_in_month(tl dates, month)
  50.             else dates_in_month(tl dates, month)
  51.         end
  52.  
  53. fun dates_in_months(dates: (int * int * int) list, months: int list) =
  54.     if null months
  55.     then []
  56.     else
  57.         if null dates
  58.         then []
  59.         else
  60.             let
  61.                 val new_dates = dates_in_month(dates, (hd months))
  62.             in
  63.                 if null new_dates
  64.                 then dates_in_months(dates, (tl months))
  65.                 else (hd new_dates) :: dates_in_months(dates, (tl months))
  66.             end
  67.  
  68. fun get_nth (strings : string list, nth: int) =
  69.     if nth = 1
  70.     then hd strings
  71.     else get_nth(tl strings, nth -1)
  72.  
  73. fun date_to_string(date: (int * int * int)) =
  74.     let
  75.         val months = [ "January", "February", "March", "April", "May",
  76.             "June", "July", "August", "September", "October",
  77.             "November", "December"
  78.         ];
  79.         val month = get_nth(months, #2 date)
  80.     in
  81.         month ^ " " ^ Int.toString(#3 date) ^ ", " ^ Int.toString(#1 date)
  82.     end
  83.  
  84.  
  85. fun number_before_reaching_sum(sum: int, positives: int list) =
  86.     let
  87.         val first = hd positives;
  88.         val new_sum = sum - first;
  89.     in
  90.         if new_sum <= 0
  91.         then 0
  92.         else 1 + number_before_reaching_sum(new_sum, (tl positives))
  93.     end
  94.  
  95. fun what_month(days: int) =
  96.     let
  97.         val days_in_month = [31, 28, 31, 30, 31,
  98.             30, 31, 31, 30, 31,
  99.             30, 31
  100.         ];
  101.     in
  102.         number_before_reaching_sum(days, days_in_month) + 1
  103.     end
  104.  
  105. fun month_range(day1: int, day2: int) =
  106.     if day2 < day1
  107.     then []
  108.     else what_month(day1) :: month_range(day1 + 1, day2)
  109.  
  110.  
  111. fun oldest(dates: (int * int * int) list) =
  112.     if null dates
  113.     then NONE
  114.     else
  115.         if null (tl dates)
  116.         then SOME (hd dates)
  117.         else
  118.             let
  119.                 fun oldest_with_at_least_two() =
  120.                     let
  121.                         val first_date = hd dates
  122.                         val second_date = hd (tl dates)
  123.                         val first_is_older = is_older(first_date, second_date);
  124.                     in
  125.                         if first_is_older
  126.                         then first_date
  127.                         else second_date
  128.                     end
  129.             in
  130.                  oldest(oldest_with_at_least_two() :: (tl (tl dates)))
  131.             end
  132.  
  133. fun remove_duplicated(numbers: int list) =
  134.     if null numbers
  135.     then []
  136.     else
  137.         if null (tl numbers)
  138.         then numbers
  139.         else
  140.             let
  141.                 val first = hd numbers;
  142.                
  143.                 fun is_in(numbers: int list) =
  144.                     if null numbers
  145.                     then false
  146.                     else
  147.                         if first = hd numbers
  148.                         then true
  149.                         else is_in(tl numbers);
  150.  
  151.             in
  152.                 if is_in (tl numbers)
  153.                 then remove_duplicated(tl numbers)
  154.                 else first :: remove_duplicated(tl numbers)
  155.             end;
  156.  
  157. fun number_in_months_challenge(dates: (int * int * int) list, months: int list) =
  158.     let
  159.         val months = remove_duplicated(months)
  160.     in
  161.         number_in_months(dates, months)
  162.     end
  163.  
  164. fun dates_in_months_challenge(dates: (int * int * int) list, months: int list) =
  165.     let
  166.         val months = remove_duplicated(months)
  167.     in
  168.         dates_in_months(dates, months)
  169.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement