From 2159410dacdddc57f6db4dbfcdd164c1e9599c53 Mon Sep 17 00:00:00 2001 From: Gregory Date: Tue, 2 Jun 2020 23:34:23 +0300 Subject: [PATCH] all done --- sml/week1/date.sml | 30 +++++++++++++++++++++++++++++- sml/week1/date_tests.sml | 33 +++++++++++++++++++++++++++++---- sml/week1/operators.sml | 8 ++++++-- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/sml/week1/date.sml b/sml/week1/date.sml index f057dca..e3dbb71 100644 --- a/sml/week1/date.sml +++ b/sml/week1/date.sml @@ -54,6 +54,22 @@ fun range from to = generate from to [] |> reverse end +fun empty lst = lst = [] + +(* not efficient but works *) +fun exists elem lst = + filter (fn needle => elem = needle) lst |> empty |> not + +fun uniqe lst = + let + fun find_uniqe elem uniqe_elems = + if (exists elem uniqe_elems) + then uniqe_elems + else elem :: uniqe_elems + in + fold find_uniqe [] lst + end + (* naive sort, will blow up the stack. Can't use pattern matching for now :( *) (* fun sort f lst = case lst of @@ -200,4 +216,16 @@ fun oldest (dates: Date list): Date option = if dates = [] then NONE else sort compare dates |> hd |> SOME - end \ No newline at end of file + end + +fun number_in_months_challenge ( + dates: Date list, + months_to_find: int list +): int = + number_in_months (dates, uniqe months_to_find) + +fun dates_in_months_challenge ( + dates: Date list, + in_months: int list +): Date list = + dates_in_months (dates, uniqe in_months) \ No newline at end of file diff --git a/sml/week1/date_tests.sml b/sml/week1/date_tests.sml index 8711ba1..93bd84c 100644 --- a/sml/week1/date_tests.sml +++ b/sml/week1/date_tests.sml @@ -1,12 +1,15 @@ use "test.sml"; use "date.sml"; - val () = assert (range 1 2 = [1, 2]) "range: generates sequence in right order" +val () = assert + (exists 1 [] = false) + "exists: empty list" + val () = assert (sort (fn a => fn b => a > b) [] = []) "sort: empty list" val () = assert (sort (fn a => fn b => a > b) [1] = [1]) "sort: one val" val () = @@ -236,7 +239,7 @@ val () = val dates = [] val expect = NONE in - assert (oldest dates = expect) "oldest: retruns NONE on empty list" + assert (oldest dates = expect) "oldest: returns NONE on empty list" end val () = @@ -244,7 +247,7 @@ val () = val dates = [(2000, 11, 31)] val expect = SOME (2000, 11, 31) in - assert (oldest dates = expect) "oldest: retruns SOME date" + assert (oldest dates = expect) "oldest: returns SOME date" end val () = @@ -252,7 +255,29 @@ val () = val dates = [(2020, 11, 31), (2021, 11, 31)] val expect = SOME (2020, 11, 31) in - assert (oldest dates = expect) "oldest: retruns oldest" + assert (oldest dates = expect) "oldest: returns oldest" + end + +val () = + let + val dates = [(2000, 11, 31), (2000, 11, 31)] + val months = [11, 11] + val expect = 2 + in + assert + (number_in_months_challenge (dates, months) = expect) + "number_in_months_challenge: ignore month duplicates" + end + +val () = + let + val dates = [(2000, 11, 31), (2000, 12, 30), (2000, 12, 31)] + val months = [11, 11] + val expect = [(2000, 11, 31)] + in + assert + (dates_in_months_challenge (dates, months) = expect) + "dates_in_months_challenge: ignore month duplicates" end val () = complete () \ No newline at end of file diff --git a/sml/week1/operators.sml b/sml/week1/operators.sml index 35a2111..0d3e6b1 100644 --- a/sml/week1/operators.sml +++ b/sml/week1/operators.sml @@ -10,8 +10,12 @@ *) fun |> (x, f) = f x -(* composition operator *) +(* apply operator *) fun $ (f, x) = f x +(* composition operator *) +fun >> (f, g) x = g(f(x)) + infix |> -infix $ \ No newline at end of file +infix $ +infix >> \ No newline at end of file