From a66b2622672cacd20571ff0c6fd0d525cff49805 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 02:16:18 +0000 Subject: [PATCH] ocaml: phase 5.1 sieve.ml baseline (Sieve of Eratosthenes) Counts primes <= 50, expected 15. Stresses the recently-added Array module + the new array-indexing syntax together with nested control flow: let sieve = Array.make (n + 1) true in sieve.(0) <- false; sieve.(1) <- false; for i = 2 to n do if sieve.(i) then begin let j = ref (i * i) in while !j <= n do sieve.(!j) <- false; j := !j + i done end done; ... Exercises: Array.make, arr.(i), arr.(i) <- v, nested for/while, begin..end blocks, ref/!/:=, integer arithmetic. 24 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/sieve.ml | 22 ++++++++++++++++++++++ plans/ocaml-on-sx.md | 4 ++++ 3 files changed, 27 insertions(+) create mode 100644 lib/ocaml/baseline/sieve.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 343c9c3b..8c254deb 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -22,6 +22,7 @@ "queens.ml": 2, "quicksort.ml": 44, "roman.ml": 44, + "sieve.ml": 15, "sum_squares.ml": 385, "word_count.ml": 3 } diff --git a/lib/ocaml/baseline/sieve.ml b/lib/ocaml/baseline/sieve.ml new file mode 100644 index 00000000..991dbcc8 --- /dev/null +++ b/lib/ocaml/baseline/sieve.ml @@ -0,0 +1,22 @@ +let count_primes n = + let sieve = Array.make (n + 1) true in + sieve.(0) <- false; + sieve.(1) <- false; + for i = 2 to n do + if sieve.(i) then begin + let j = ref (i * i) in + while !j <= n do + sieve.(!j) <- false; + j := !j + i + done + end + done; + let count = ref 0 in + for i = 2 to n do + if sieve.(i) then count := !count + 1 + done; + !count + +;; + +count_primes 50 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 3163d670..9c6c2e00 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,10 @@ _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 — sieve.ml baseline (Sieve of Eratosthenes, + count of primes ≤ 50 = 15). Stresses Array.make + arr.(i) + + arr.(i) <- v + nested for/while loops + `begin..end` block. 24 + baseline programs total. - 2026-05-09 Phase 4 — `arr.(i)` and `arr.(i) <- v` array indexing syntax (+3 tests, 515 total). parse-atom-postfix's `.(...)` branch now disambiguates between let-open and array-get based on whether