add comments and tests

This commit is contained in:
SowinskiBraeden committed 2026-01-29 16:13:37 -08:00
1 parent 1b59619480
commit b6570114cb
2 files changed
+182 -14

No files matched your search

+27
View File
@@ -1,3 +1,5 @@
(** [digits n] takes a positive integer [n] and returns
a list of integers of each digit in the number [n] *)
let digits n =
let rec digits' n acc =
if n = 0 then acc
@@ -5,5 +7,30 @@ let digits n =
in
digits' n [];;
(**/**)
let test_digits () =
assert (digits 0 = []);
assert (digits 123 = [1;2;3]);
assert (digits 123040 = [1;2;3;0;4;0])
(**/**)
(** [int_of_digits d] takes a list of positive integers [d]
and returns a single number where each digit is from the
list [d]
*)
let int_of_digits d =
(List.fold_left (fun x acc -> (x + acc) * 10) 0 d) / 10;;
(**/**)
let test_int_of_digits () =
assert (int_of_digits [] = 0);
assert (int_of_digits [0] = 0);
assert (int_of_digits [0;0;1;2;3] = 123);
assert (int_of_digits [2;0;0;5] = 2005)
(**/**)
(**/**)
let run_all_tests () =
test_digits();
test_int_of_digits()
(**/**)