Skip to content

Instantly share code, notes, and snippets.

@qzdc00
Last active August 29, 2015 14:10
Show Gist options
  • Save qzdc00/bbd182ca6413cfe4050e to your computer and use it in GitHub Desktop.
Save qzdc00/bbd182ca6413cfe4050e to your computer and use it in GitHub Desktop.
hw1/WashingtonPLMOOC
fun is_older(date1 : int*int*int, date2: int*int*int) =
if #1 date1 <> #1 date2
then
if #1 date1 < #1 date2
then true
else false
else
if #2 date1 <> #2 date2
then
if #2 date1 < #2 date2
then true
else false
else
if #3 date1 < #3 date2
then true
else false
fun number_in_month(dates : (int * int * int) list, month : int) =
if null dates
then 0
else
let
val cnt = number_in_month(tl dates, month)
in
if #2 (hd dates) = month
then cnt + 1
else cnt
end
fun number_in_months(dates : (int * int * int) list, months : int list) =
if null months
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 l = dates_in_month(tl dates, month)
in
if #2 (hd dates) = month
then (hd dates) :: l
else l
end
fun dates_in_months(dates : (int * int * int) list, months : int list) =
if null months
then []
else dates_in_month(dates, hd months) @ dates_in_months(dates, tl months)
fun get_nth(strs : string list, n : int) =
if n = 1
then hd strs
else get_nth(tl strs, n - 1)
fun date_to_string(date : int * int * int) =
let
val months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
in
get_nth(months, #2 date) ^ " " ^ Int.toString(#3 date) ^ ", " ^ Int.toString(#1 date)
end
(*
//standard version
fun number_before_reaching_sum (sum : int, lst : int list) =
if sum <= hd lst
then 0
else 1 + number_before_reaching_sum(sum - hd lst, tl lst)*)
fun number_before_reaching_sum(sum : int, posInts : int list) =
let
fun aux(s : int, ints : int list, cnt : int) =
let
val firstInt = hd ints
val secondInt = hd (tl ints)
in
if cnt = 1 andalso s <= firstInt
then 0
else
if s > firstInt andalso s - firstInt > secondInt
then aux(s - firstInt, tl ints, cnt + 1)
else cnt
end
in
aux(sum, posInts, 1)
end
fun what_month(day : int) =
let
val ints = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
in
number_before_reaching_sum(day, ints) + 1
end
fun month_range(day1 : int, day2 : int) =
if day1 > day2
then []
else what_month(day1) :: month_range(day1 + 1, day2)
fun oldest(dates : (int * int * int) list) =
if null dates
then NONE
else
let
val old = oldest(tl dates)
in
if isSome(old) andalso is_older(valOf(old), hd dates)
then old
else SOME(hd dates)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment