From b8dfc080dd90fb183857f129c70ae15edf64ce9f Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 11:33:30 +0000 Subject: [PATCH] ocaml: phase 5.1 zip_unzip.ml baseline (zip/unzip round-trip, sum-product = 1000) zip walks both lists in lockstep, truncating at the shorter. unzip uses tuple-pattern destructuring on the recursive result. let pairs = zip [1;2;3;4] [10;20;30;40] in let (xs, ys) = unzip pairs in List.fold_left (+) 0 xs * List.fold_left (+) 0 ys = 10 * 100 = 1000 Exercises: - tuple-cons patterns in match scrutinee: 'match (xs, ys) with' - tuple constructor in return value: '(a :: la, b :: lb)' - the iter-98 let-tuple destructuring: 'let (la, lb) = unzip rest' 53 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/zip_unzip.ml | 18 ++++++++++++++++++ plans/ocaml-on-sx.md | 9 +++++++++ 3 files changed, 28 insertions(+) create mode 100644 lib/ocaml/baseline/zip_unzip.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 3f93f13d..2f291158 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -48,6 +48,7 @@ "safe_div.ml": 20, "shuffle.ml": 55, "word_freq.ml": 8, + "zip_unzip.ml": 1000, "sieve.ml": 15, "sum_squares.ml": 385, "unique_set.ml": 9, diff --git a/lib/ocaml/baseline/zip_unzip.ml b/lib/ocaml/baseline/zip_unzip.ml new file mode 100644 index 00000000..b64787db --- /dev/null +++ b/lib/ocaml/baseline/zip_unzip.ml @@ -0,0 +1,18 @@ +let rec zip xs ys = + match (xs, ys) with + | ([], _) -> [] + | (_, []) -> [] + | (x :: xs', y :: ys') -> (x, y) :: zip xs' ys' + +let rec unzip pairs = + match pairs with + | [] -> ([], []) + | (a, b) :: rest -> + let (la, lb) = unzip rest in + (a :: la, b :: lb) + +;; + +let pairs = zip [1;2;3;4] [10;20;30;40] in +let (xs, ys) = unzip pairs in +List.fold_left (+) 0 xs * List.fold_left (+) 0 ys diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 8fdf4adc..82eee160 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _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 — zip_unzip.ml baseline (list zip/unzip + round-trip, sum-product = 1000). zip walks both lists in lockstep + truncating at the shorter; unzip uses tuple-pattern destructuring + on the recursive result. After zip [1;2;3;4] [10;20;30;40] + + unzip, sums are 10 and 100 → product 1000. Exercises tuple-cons + patterns in match scrutinee `(xs, ys)`, tuple constructor in + return value `(a :: la, b :: lb)`, and the iter-98 let-tuple + destructuring `let (la, lb) = unzip rest in`. 53 baseline programs + total. - 2026-05-09 Phase 5.1 — bigint_add.ml baseline (digit-list big-num add, 28 = 1+18+9). Recursive 4-arm match on `(a, b)` tuples threading a carry: `(x::xs, y::ys) -> (s mod 10) :: aux xs ys (s