diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 9e36ca4a..b076242f 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -29,6 +29,7 @@ "quicksort.ml": 44, "roman.ml": 44, "safe_div.ml": 20, + "shuffle.ml": 55, "sieve.ml": 15, "sum_squares.ml": 385, "unique_set.ml": 9, diff --git a/lib/ocaml/baseline/shuffle.ml b/lib/ocaml/baseline/shuffle.ml new file mode 100644 index 00000000..2ea776d8 --- /dev/null +++ b/lib/ocaml/baseline/shuffle.ml @@ -0,0 +1,15 @@ +let shuffle xs = + Random.init 42; + let a = Array.of_list xs in + let n = Array.length a in + for i = n - 1 downto 1 do + let j = Random.int (i + 1) in + let tmp = a.(i) in + a.(i) <- a.(j); + a.(j) <- tmp + done; + Array.to_list a + +;; + +List.fold_left (+) 0 (shuffle [1;2;3;4;5;6;7;8;9;10]) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 5c8df858..7ac4126c 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _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 — shuffle.ml baseline (Fisher-Yates with + deterministic Random.init seed). In-place swap loop using `for i = + n - 1 downto 1` and `a.(i) <- a.(j)`. Sum is invariant under + permutation, so the test value (55 for [1..10]) verifies that the + shuffle is a valid permutation regardless of which one. Exercises + Random.init / Random.int + Array.of_list / to_list / length / + arr.(i) / arr.(i) <- v + downto loop. 33 baseline programs total. - 2026-05-09 Phase 5.1 — pi_leibniz.ml baseline (Leibniz formula, 1000 terms × 100 → 314). Side-quest: `int_of_float` was wrong — defined as identity in iteration 94 instead of truncation. Fixed