From da6d8e39c982a3e0b9fcfe904cbed6b848c76510 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 09:57:18 +0000 Subject: [PATCH] ocaml: phase 5.1 pascal.ml baseline (Pascal triangle row 10 middle = C(10,5) = 252) next_row prepends 1, walks adjacent pairs (x, y) emitting x+y, appends a final 1: let rec next_row prev = let rec aux a = match a with | [_] -> [1] | x :: y :: rest -> (x + y) :: aux (y :: rest) | [] -> [] in 1 :: aux prev row n iterates next_row n times starting from [1] using a ref + 'for _ = 1 to n do r := next_row !r done'. row 10 = [1;10;45;120;210;252;210;120;45;10;1] List.nth (row 10) 5 = 252 = C(10, 5) Exercises three-arm match including [_] singleton wildcard, x :: y :: rest binding, and the for-loop with wildcard counter. 45 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/pascal.ml | 19 +++++++++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 27 insertions(+) create mode 100644 lib/ocaml/baseline/pascal.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index d4382a4c..f0724929 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -29,6 +29,7 @@ "newton_sqrt.ml": 1414, "mutable_record.ml": 10, "option_match.ml": 5, + "pascal.ml": 252, "pi_leibniz.ml": 314, "pretty_table.ml": 64, "poly_stack.ml": 5, diff --git a/lib/ocaml/baseline/pascal.ml b/lib/ocaml/baseline/pascal.ml new file mode 100644 index 00000000..b421f791 --- /dev/null +++ b/lib/ocaml/baseline/pascal.ml @@ -0,0 +1,19 @@ +let rec next_row prev = + let rec aux a = + match a with + | [_] -> [1] + | x :: y :: rest -> (x + y) :: aux (y :: rest) + | [] -> [] + in + 1 :: aux prev + +let row n = + let r = ref [1] in + for _ = 1 to n do + r := next_row !r + done; + !r + +;; + +List.nth (row 10) 5 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index f0fd71d6..e263bf85 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 — pascal.ml baseline (Pascal's triangle row + 10 middle = C(10, 5) = 252). next_row prepends 1, walks adjacent + pairs (x, y) emitting x+y, appends a final 1. row n iterates + next_row n times starting from [1]. Three-arm match including + `[_]` (singleton wildcard) and `x :: y :: rest`. Iteration via + `for _ = 1 to n do r := next_row !r done`. 45 baseline programs + total. - 2026-05-09 Phase 5.1 — run_length.ml baseline (run-length encoding, sum of counts = 11). RLE encodes [1;1;1;2;2;3;3;3;3;1;1] as [(1,3);(2,2);(3,4);(1,2)]. Sum-of-counts = 11 verifies that the