all done
This commit is contained in:
parent
6c514e5fc2
commit
2159410dac
3 changed files with 64 additions and 7 deletions
|
@ -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
|
||||
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)
|
|
@ -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 ()
|
|
@ -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 $
|
||||
infix $
|
||||
infix >>
|
Loading…
Reference in a new issue