From 59287df01cc51c16b605414ee166a48ec66acc53 Mon Sep 17 00:00:00 2001 From: Gregory Date: Mon, 1 Jun 2020 01:55:44 +0300 Subject: [PATCH] number_in_months done --- sml/week1/date.sml | 14 +++++++ sml/week1/date_tests.sml | 79 +++++++++++++++++++++++++++++++--------- 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/sml/week1/date.sml b/sml/week1/date.sml index c527ac9..64d4634 100644 --- a/sml/week1/date.sml +++ b/sml/week1/date.sml @@ -1,6 +1,10 @@ (* year * month * day *) type Date = int * int * int +fun |> (x, f) = f x + +infix |> + fun fold f lst acc = case lst of [] => acc @@ -30,3 +34,13 @@ fun number_in_month ( fold count_month dates 0 end +fun number_in_months ( + dates: Date list, + months_to_find: int list +): int = + let + fun count_months month acc = acc + number_in_month (dates, month) + in + fold count_months months_to_find 0 + end + diff --git a/sml/week1/date_tests.sml b/sml/week1/date_tests.sml index 2aec7c7..4a6de89 100644 --- a/sml/week1/date_tests.sml +++ b/sml/week1/date_tests.sml @@ -1,57 +1,100 @@ use "test.sml"; use "date.sml"; - -val () = - assert +val () = + assert (is_older ((1999, 12, 31), (1999, 12, 31)) = false) "is_older: same dates evaluates to false" -val () = - assert +val () = + assert (is_older ((2000, 11, 28), (2000, 11, 29)) = true) "is_older: first date older than second by day" -val () = - assert +val () = + assert (is_older ((1999, 12, 31), (1999, 11, 31)) = false) "is_older: first date greater by month" -val () = - assert +val () = + assert (is_older ((1999, 12, 31), (1999, 11, 31)) = false) "is_older: first date greater by month" -val () = - assert +val () = + assert (is_older ((2000, 12, 31), (1999, 12, 31)) = false) "is_older: first date greater by year" -val () = - assert +val () = + assert (number_in_month ([(2000, 12, 31)], 12) = 1) "number_in_month: one date with exact month" -val () = +val () = let val dates = [(2000, 12, 31), (2000, 12, 31)] val month = 12 val expect = 2 in - assert + assert (number_in_month (dates, month) = expect) "number_in_month: two date with exact month" end -val () = +val () = + let + val dates = [(2000, 11, 31), (2000, 12, 31)] + val month = 11 + val expect = 1 + in + assert + (number_in_month (dates, month) = expect) + "number_in_month: two date but only one matches" + end + +val () = let val dates = [] val month = 12 val expect = 0 in - assert + assert (number_in_month (dates, month) = expect) - "number_in_month: empty list" + "number_in_month: empty list should return 0" + end + +val () = + let + val dates = [] + val months = [] + val expect = 0 + in + assert + (number_in_months (dates, months) = expect) + "number_in_months: empty list should return 0" + end + +val () = + let + val dates = [(2000, 11, 31), (2000, 11, 31)] + val months = [11] + val expect = 2 + in + assert + (number_in_months (dates, months) = expect) + "number_in_months: one month matches with two dates" + end + +val () = + let + val dates = [(2000, 11, 31), (2000, 12, 31), (2000, 12, 31)] + val months = [11, 12] + val expect = 3 + in + assert + (number_in_months (dates, months) = expect) + "number_in_months: multiple dates with same month" end val () = complete () \ No newline at end of file