ocaml: phase 5.1 zip_unzip.ml baseline (zip/unzip round-trip, sum-product = 1000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s

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.
This commit is contained in:
2026-05-09 11:33:30 +00:00
parent ac19b7aced
commit b8dfc080dd
3 changed files with 28 additions and 0 deletions

View File

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

View File

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