Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created January 26, 2013 17:46
Show Gist options
  • Save v2e4lisp/4643469 to your computer and use it in GitHub Desktop.
Save v2e4lisp/4643469 to your computer and use it in GitHub Desktop.
programming languanges (coursera) week-1 homework.
(*------------------------ WEEK 1 ------------------------------------*)
(* 1 *)
fun is_older (fst: int*int*int, snd: int*int*int) =
(#1 fst) * 10000 + (#2 fst) * 100 + #3 fst <
(#1 snd) * 10000 + (#2 snd) * 100 + #3 snd
(* 2 *)
fun number_in_month (dates: (int*int*int) list, month: int) =
if null dates then 0
else if #2 (hd dates) = month then 1 + number_in_month (tl dates, month)
else number_in_month (tl dates, month);
(* 3 *)
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);
(* 4 *)
fun dates_in_month (dates: (int*int*int) list, month) =
if null dates then []
else
let
val date = hd dates
in
if #2 date = month then date:: dates_in_month (tl dates, month)
else dates_in_month (tl dates, month)
end;
(* 5 *)
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);
(* 6 *)
fun get_nth (strings: string list, n: int) =
if n = 1 then hd strings
else get_nth (tl strings, n-1);
(* 7 *)
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;
(* 8 *)
fun number_before_reaching_sum (sum: int, numbers: int list) =
let
fun helper (total: int, index: int, numbers: int list) =
if total >= sum then index - 1
else helper (total + (hd numbers), index + 1, tl numbers)
in
helper (hd numbers, 1, tl numbers)
end;
(* 9 *)
fun what_month (day: int) =
1 + number_before_reaching_sum (day, [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31]);
(* 10 *)
fun month_range (day1: int, day2: int) =
if day1 > day2 then []
else what_month day1 :: month_range (day1+1, day2);
(* 11 *)
fun oldest (dates: (int*int*int) list) =
if null dates then NONE
else
let
fun helper (date: (int*int*int), dates: (int*int*int) list) =
if null dates then date
else if is_older (date, hd dates) then helper (date, tl dates)
else helper (hd dates, tl dates);
in
SOME (helper (hd dates, tl dates))
end;
(* 12 *)
fun number_in_months_challenge (dates: (int*int*int) list, months: int list) =
if null months then 0
else
let
fun is_in (item: int, alist: int list) =
if null alist then false
else if item = hd alist then true
else is_in (item, alist);
fun remove_duplicates (alist: int list) =
if null alist then alist
else if is_in (hd alist, tl alist) then
remove_duplicates (tl alist)
else
hd alist :: remove_duplicates (tl alist)
val months = remove_duplicates months
in
number_in_months (dates, months)
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment