diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 367541d2..6a61cd43 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -14,6 +14,7 @@ "factorial.ml": 3628800, "fraction.ml": 7, "frequency.ml": 5, + "hanoi.ml": 1023, "fizzbuzz.ml": 57, "list_ops.ml": 30, "json_pretty.ml": 24, diff --git a/lib/ocaml/baseline/hanoi.ml b/lib/ocaml/baseline/hanoi.ml new file mode 100644 index 00000000..a57cc1f5 --- /dev/null +++ b/lib/ocaml/baseline/hanoi.ml @@ -0,0 +1,11 @@ +let rec hanoi n from to_ via = + if n = 0 then 0 + else + let a = hanoi (n - 1) from via to_ in + let b = 1 in + let c = hanoi (n - 1) via to_ from in + a + b + c + +;; + +hanoi 10 1 3 2 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 7ede6105..3e799773 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — hanoi.ml baseline (Tower of Hanoi move + count, n=10 → 1023). Classic doubly-recursive solution returning + the number of moves: `hanoi n from to via = hanoi (n-1) from via + to + 1 + hanoi (n-1) via to from`. Counts to 2^10 - 1 = 1023 for + n=10, exercising tail-position addition + 4-arg recursion + + conditional base case. (Uses `to_` instead of `to` to avoid + collision with the `to` keyword in for-loops — OCaml conventional + workaround.) 37 baseline programs total. - 2026-05-09 Phase 5.1 — validate.ml baseline (Either-based input validation, 3 errors × 100 + 117 sum = 417). validate_int returns `Left msg` on empty / non-digit, `Right (int_of_string s)` on a