ocaml: phase 5.1 zigzag.ml baseline (interleave two lists, sum 1..10 = 55)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

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.
This commit is contained in:
2026-05-09 14:30:55 +00:00
parent 0f2eb45f5c
commit a2f3c533b8
3 changed files with 15 additions and 0 deletions

View File

@@ -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,

View File

@@ -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])

View File

@@ -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