Advertisement
Guest User

Untitled

a guest
Sep 4th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun is_older (past : int*int*int, future : int*int*int) =
  2.     if #1 future > #1 past then true
  3.     else
  4.         if #1 future < #1 past
  5.         then false
  6.         else if #2 future > #2 past
  7.              then true
  8.              else if #2 future < #2 past
  9.                 then false
  10.                 else if #3 future > #3 past
  11.                     then true
  12.                     else false
  13.                    
  14. fun number_in_month(dates : (int*int*int)list, month:int) =
  15.     if null dates
  16.     then 0
  17.     else if #2 ( hd(dates) ) = month
  18.         then 1 + number_in_month( tl(dates) , month)
  19.         else number_in_month( tl(dates) , month)
  20.        
  21.    
  22. fun number_in_months(dates: (int*int*int)list, months: int list) =
  23.     if null months
  24.     then 0
  25.     else number_in_month(dates,(hd months)) + number_in_months(dates, (tl months))
  26.  
  27. fun dates_in_month(dates: (int*int*int)list, month:int) =
  28.     if null dates
  29.     then []
  30.     else if #2 ( hd(dates) ) = month
  31.         then (hd dates) :: (dates_in_month( tl(dates) , month))
  32.         else dates_in_month( tl(dates) , month)
  33.  
  34. fun dates_in_months(dates: (int*int*int)list, months:int list) =
  35.     if null months
  36.     then []
  37.     else dates_in_month(dates,(hd months)) @ dates_in_months(dates, (tl months))
  38.  
  39. fun  get_nth(w : string list, n : int) =
  40.     if n = 1
  41.     then hd w
  42.     else get_nth((tl w), n-1)
  43.  
  44. fun date_to_string(date : (int*int*int)) =
  45.     let
  46.        val months = ["January", "February", "March", "April",
  47.         "May", "June", "July", "August", "September", "October", "November", "December"]
  48.     in
  49.         get_nth(months, #2 date)  ^ " " ^  Int.toString (#3 date) ^ ", " ^ Int.toString (#1 date)
  50.     end
  51.  
  52. fun number_before_reaching_sum (sum : int, num : int list) =
  53.     let
  54.         fun beforeSum (l : int list, i : int, s : int) =
  55.             if s < sum
  56.             then beforeSum (tl l, i + 1, s + hd l)
  57.             else i - 1
  58.     in
  59.         beforeSum(num, 0, 0)
  60.     end
  61.    
  62. fun what_month (day : int) =
  63.     let
  64.        val months = [31,28, 31,30,31,30,31,31,30,31,30,31]
  65.     in
  66.        number_before_reaching_sum(day, months) + 1
  67.     end
  68.  
  69. fun oldest (days : (int*int*int)list) =
  70.     if null days
  71.     then NONE
  72.     else
  73.         let val tl_d = oldest (tl days)
  74.         in
  75.            if isSome tl_d andalso is_older ( valOf tl_d, hd days)
  76.            then tl_d
  77.            else SOME (hd days)
  78.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement