From a2f3c533b817b5658c25df6f76fc920319adfc63 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 14:30:55 +0000 Subject: [PATCH] ocaml: phase 5.1 zigzag.ml baseline (interleave two lists, sum 1..10 = 55) One-liner that swaps the lists on every recursive call: let rec zigzag xs ys = match xs with | [] -> ys | x :: xs' -> x :: zigzag ys xs' This works because each call emits the head of xs and recurses with ys as the new xs and the rest of xs as the new ys. zigzag [1;3;5;7;9] [2;4;6;8;10] = [1;2;3;4;5;6;7;8;9;10] sum = 55 Tests recursive list cons + arg-swap idiom that is concise but non-obvious to readers expecting symmetric-handling. 68 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/zigzag.ml | 8 ++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 15 insertions(+) create mode 100644 lib/ocaml/baseline/zigzag.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 4d72e60d..208228be 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -61,6 +61,7 @@ "subset_sum.ml": 8, "tic_tac_toe.ml": 1, "word_freq.ml": 8, + "zigzag.ml": 55, "zip_unzip.ml": 1000, "sieve.ml": 15, "sum_squares.ml": 385, diff --git a/lib/ocaml/baseline/zigzag.ml b/lib/ocaml/baseline/zigzag.ml new file mode 100644 index 00000000..710644f0 --- /dev/null +++ b/lib/ocaml/baseline/zigzag.ml @@ -0,0 +1,8 @@ +let rec zigzag xs ys = + match xs with + | [] -> ys + | x :: xs' -> x :: zigzag ys xs' + +;; + +List.fold_left (+) 0 (zigzag [1;3;5;7;9] [2;4;6;8;10]) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 153ed59f..09bcf0a2 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _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 — zigzag.ml baseline (interleave two lists, + sum 1..10 = 55). One-liner that swaps the lists on every recursive + call: `match xs with [] -> ys | x :: xs' -> x :: zigzag ys xs'`. + zigzag [1;3;5;7;9] [2;4;6;8;10] = [1;2;3;4;5;6;7;8;9;10] sum 55. + Tests recursive list cons + arg-swap idiom. 68 baseline programs + total. - 2026-05-09 Phase 5.1 — prefix_sum.ml baseline (precomputed prefix sums for O(1) range queries, sum of three queries = 66). prefix_sums xs returns an Array of len n+1 such that