number_in_months done

This commit is contained in:
Gregory 2020-06-01 01:55:44 +03:00
parent e2648e3cf6
commit 59287df01c
2 changed files with 75 additions and 18 deletions

View file

@ -1,6 +1,10 @@
(* year * month * day *) (* year * month * day *)
type Date = int * int * int type Date = int * int * int
fun |> (x, f) = f x
infix |>
fun fold f lst acc = fun fold f lst acc =
case lst of case lst of
[] => acc [] => acc
@ -30,3 +34,13 @@ fun number_in_month (
fold count_month dates 0 fold count_month dates 0
end 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

View file

@ -1,7 +1,6 @@
use "test.sml"; use "test.sml";
use "date.sml"; use "date.sml";
val () = val () =
assert assert
(is_older ((1999, 12, 31), (1999, 12, 31)) = false) (is_older ((1999, 12, 31), (1999, 12, 31)) = false)
@ -43,6 +42,17 @@ val () =
"number_in_month: two date with exact month" "number_in_month: two date with exact month"
end end
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 () = val () =
let let
val dates = [] val dates = []
@ -51,7 +61,40 @@ val () =
in in
assert assert
(number_in_month (dates, month) = expect) (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 end
val () = complete () val () = complete ()