diff options
| -rw-r--r-- | challenge-075/shawn-wagner/ocaml/ch-1.ml | 28 |
1 files changed, 28 insertions, 0 deletions
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 + |
