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