This commit is contained in:
Gregory 2020-06-02 23:34:23 +03:00
parent 6c514e5fc2
commit 2159410dac
3 changed files with 64 additions and 7 deletions

View file

@ -54,6 +54,22 @@ fun range from to =
generate from to [] |> reverse generate from to [] |> reverse
end 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 :( *) (* naive sort, will blow up the stack. Can't use pattern matching for now :( *)
(* fun sort f lst = (* fun sort f lst =
case lst of case lst of
@ -200,4 +216,16 @@ fun oldest (dates: Date list): Date option =
if dates = [] if dates = []
then NONE then NONE
else sort compare dates |> hd |> SOME else sort compare dates |> hd |> SOME
end 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)

View file

@ -1,12 +1,15 @@
use "test.sml"; use "test.sml";
use "date.sml"; use "date.sml";
val () = val () =
assert assert
(range 1 2 = [1, 2]) (range 1 2 = [1, 2])
"range: generates sequence in right order" "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) [] = []) "sort: empty list"
val () = assert (sort (fn a => fn b => a > b) [1] = [1]) "sort: one val" val () = assert (sort (fn a => fn b => a > b) [1] = [1]) "sort: one val"
val () = val () =
@ -236,7 +239,7 @@ val () =
val dates = [] val dates = []
val expect = NONE val expect = NONE
in in
assert (oldest dates = expect) "oldest: retruns NONE on empty list" assert (oldest dates = expect) "oldest: returns NONE on empty list"
end end
val () = val () =
@ -244,7 +247,7 @@ val () =
val dates = [(2000, 11, 31)] val dates = [(2000, 11, 31)]
val expect = SOME (2000, 11, 31) val expect = SOME (2000, 11, 31)
in in
assert (oldest dates = expect) "oldest: retruns SOME date" assert (oldest dates = expect) "oldest: returns SOME date"
end end
val () = val () =
@ -252,7 +255,29 @@ val () =
val dates = [(2020, 11, 31), (2021, 11, 31)] val dates = [(2020, 11, 31), (2021, 11, 31)]
val expect = SOME (2020, 11, 31) val expect = SOME (2020, 11, 31)
in 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 end
val () = complete () val () = complete ()

View file

@ -10,8 +10,12 @@
*) *)
fun |> (x, f) = f x fun |> (x, f) = f x
(* composition operator *) (* apply operator *)
fun $ (f, x) = f x fun $ (f, x) = f x
(* composition operator *)
fun >> (f, g) x = g(f(x))
infix |> infix |>
infix $ infix $
infix >>