From faabd66e0dfbb34f93f5fd65112c96cadd5887ad Mon Sep 17 00:00:00 2001 From: Shawn Date: Sat, 29 Aug 2020 10:19:55 -0700 Subject: Challenge 075 task1 solution in ocaml --- challenge-075/shawn-wagner/ocaml/ch-1.ml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-075/shawn-wagner/ocaml/ch-1.ml diff --git a/challenge-075/shawn-wagner/ocaml/ch-1.ml b/challenge-075/shawn-wagner/ocaml/ch-1.ml new file mode 100644 index 0000000000..05d4052ff0 --- /dev/null +++ b/challenge-075/shawn-wagner/ocaml/ch-1.ml @@ -0,0 +1,28 @@ +let sum = List.fold_left (+) 0 + +let rec solve coins s = + if s = 0 then + [[]] + else begin + let solutions = ref [] in + List.iter (function coin -> + let f solution = if (sum solution) + coin = s then + Some (coin :: solution) + else + None in + if s - coin >= 0 then + solutions := + List.append (List.filter_map f (solve coins (s - coin))) + !solutions) coins; + !solutions + end + +let task1 coins s = + let solutions= solve coins s in + let solutions = List.map (List.sort Int.compare) solutions in + let solutions = List.sort_uniq compare solutions in + Printf.printf "There are %d possible ways to make sum %d\n" (List.length solutions) s + +let _ = + task1 [1;2;4] 6 + -- cgit