Disjoint-set union with path compression:
let make_uf n = Array.init n (fun i -> i)
let rec find p x =
if p.(x) = x then x
else begin let r = find p p.(x) in p.(x) <- r; r end
let union p x y =
let rx = find p x in let ry = find p y in
if rx <> ry then p.(rx) <- ry
After unioning (0,1), (2,3), (4,5), (6,7), (0,2), (4,6):
{0,1,2,3} {4,5,6,7} {8} {9} --> 4 components.
Tests Array.init with closure, recursive find, in-place .(i)<-r.
139 baseline programs total.
Hoare quickselect with Lomuto partition: recursively narrows the
range to whichever side contains the kth index. Mutates the array
in place via .(i)<-v. The median (k=4) of [7;2;9;1;5;6;3;8;4] is 5.
let rec quickselect arr lo hi k =
if lo = hi then arr.(lo)
else begin
let pivot = arr.(hi) in
let i = ref lo in
for j = lo to hi - 1 do
if arr.(j) < pivot then begin
let t = arr.(!i) in
arr.(!i) <- arr.(j); arr.(j) <- t;
i := !i + 1
end
done;
...
end
Exercises array literal syntax + in-place mutation in the same
program, ensuring [|...|] yields a mutable backing.
138 baseline programs total.
Added parser support for OCaml array literal syntax:
[| e1; e2; ...; en |] --> Array.of_list [e1; e2; ...; en]
[||] --> Array.of_list []
Desugaring keeps the array representation unchanged (ref-of-list)
since Array.of_list is a no-op constructor for that backing.
Tokenizer emits [, |, |, ] as separate ops; parser detects [ followed
by | and enters array-literal mode, terminating on |].
Baseline lis.ml exercises the syntax:
let lis arr =
let n = Array.length arr in
let dp = Array.make n 1 in
for i = 1 to n - 1 do
for j = 0 to i - 1 do
if arr.(j) < arr.(i) && dp.(j) + 1 > dp.(i) then
dp.(i) <- dp.(j) + 1
done
done;
let best = ref 0 in
for i = 0 to n - 1 do
if dp.(i) > !best then best := dp.(i)
done;
!best
lis [|10; 22; 9; 33; 21; 50; 41; 60; 80|] = 6
137 baseline programs total.
Classic Josephus problem solved with the standard recurrence:
let rec josephus n k =
if n = 1 then 0
else (josephus (n - 1) k + k) mod n
josephus 50 3 + 1 = 11
50 people stand in a circle, every 3rd is eliminated; the last
survivor is at position 11 (1-indexed). Tests recursion + mod.
136 baseline programs total.
Counts integer partitions via classic DP:
let partition_count n =
let dp = Array.make (n + 1) 0 in
dp.(0) <- 1;
for k = 1 to n do
for i = k to n do
dp.(i) <- dp.(i) + dp.(i - k)
done
done;
dp.(n)
partition_count 15 = 176
Tests Array.make, .(i)<-/.(i) array access, nested for-loops, refs.
135 baseline programs total.
Uses Euclid's formula: for coprime m > k of opposite parity, the
triple (m^2 - k^2, 2mk, m^2 + k^2) is a primitive Pythagorean.
let count_primitive_triples n =
let c = ref 0 in
for m = 2 to 50 do
let kk = ref 1 in
while !kk < m do
if (m - !kk) mod 2 = 1 && gcd m !kk = 1 then begin
let h = m * m + !kk * !kk in
if h <= n then c := !c + 1
end;
kk := !kk + 1
done
done;
!c
count_primitive_triples 100 = 16
The 16 triples include the classics (3,4,5), (5,12,13), (8,15,17),
(7,24,25), and end with (65,72,97).
134 baseline programs total.
A Harshad (or Niven) number is divisible by its digit sum:
let count_harshad limit =
let c = ref 0 in
for n = 1 to limit do
if n mod (digit_sum n) = 0 then c := !c + 1
done;
!c
count_harshad 100 = 33
All single-digit numbers (1..9) qualify trivially. Plus 10, 12, 18,
20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80,
81, 84, 90, 100 (24 more) = 33 total under 100.
133 baseline programs total.
Walks digits via mod 10 / div 10, accumulating the reversed value:
let reverse_int n =
let m = ref n in
let r = ref 0 in
while !m > 0 do
r := !r * 10 + !m mod 10;
m := !m / 10
done;
!r
reverse 12345 + reverse 100 + reverse 7
= 54321 + 1 + 7
= 54329
Trailing zeros collapse (reverse 100 = 1, not 001).
132 baseline programs total.
Walks the pin-knockdown list applying strike/spare bonuses through
a 10-frame counter:
strike (10): score 10 + next 2 throws, advance i+1
spare (a + b = 10): score 10 + next 1 throw, advance i+2
open (a + b < 10): score a + b, advance i+2
Frame ten special-cases are handled implicitly: the input includes
bonus throws naturally and the while-loop terminates after frame 10.
bowling_score [10; 7; 3; 9; 0; 10; 0; 8; 8; 2; 0; 6;
10; 10; 10; 8; 1]
= 20+19+9+18+8+10+6+30+28+19
= 167
131 baseline programs total.
Single-helper tail-recursive loop threading an accumulator:
let factorial n =
let rec go n acc =
if n <= 1 then acc
else go (n - 1) (n * acc)
in
go n 1
factorial 12 = 479_001_600
Companion to factorial.ml (10! = 3628800 via doubly-recursive
style); same answer-shape, different evaluator stress: this version
has constant stack depth.
130 baseline programs total — milestone.
safe_div returns None on division by zero; safe_chain stitches two
divisions, propagating None on either failure:
let safe_div a b =
if b = 0 then None else Some (a / b)
let safe_chain a b c =
match safe_div a b with
| None -> None
| Some q -> safe_div q c
Test:
safe_chain 100 2 5 = Some 10
safe_chain 100 0 5 = None -> -1
safe_chain 50 5 0 = None -> -1
safe_chain 1000 10 5 = Some 20
10 - 1 - 1 + 20 = 28
Tests Option chaining + match-on-result with sentinel default.
Demonstrates the canonical 'fail-early on None' pattern.
129 baseline programs total.
19-arm match returning the English word for each number 1..19, then
sum String.length:
let number_to_words n =
match n with
| 1 -> 'one' | 2 -> 'two' | ... | 19 -> 'nineteen'
| _ -> ''
total_letters 19 = 36 + 70 = 106
(1-9) (10-19)
Real PE17 covers 1..1000 (answer 21124) but needs more elaborate
number-to-words logic (compounds, 'and', 'thousand'). 1..19 keeps
the program small while exercising literal-pattern match dispatch
on many arms.
128 baseline programs total.
let palindrome_sum lo hi =
let total = ref 0 in
for n = lo to hi do
if is_pal n then total := !total + n
done;
!total
palindrome_sum 100 999 = 49500
There are 90 three-digit palindromes (form aba; 9 choices for a, 10
for b). Average value 550, sum 49500.
Companion to palindrome.ml (predicate-only) and paren_depth.ml.
127 baseline programs total.
PE12 with target = 10:
let count_divisors n =
let c = ref 0 in
let i = ref 1 in
while !i * !i <= n do
if n mod !i = 0 then begin
c := !c + 1;
if !i * !i <> n then c := !c + 1
end;
i := !i + 1
done;
!c
let first_triangle_with_divs target =
walk triangles T(n) = T(n-1) + n until count_divisors T > target
T(15) = 120 has 16 divisors — first to exceed 10. Real PE12 uses
target 500 (answer 76576500); 10 stays well under budget.
126 baseline programs total.
Perfect numbers = those where the proper-divisor sum equals n. Three
exist under 500: 6, 28, 496. (8128 is the next; 33550336 the one
after that.)
Same div_sum machinery as euler21_small.ml / abundant.ml (the
trial-division up to sqrt-n).
Original 10000 limit timed out at 10 minutes under contention (496
itself takes thousands of trials at the inner loop). 500 stays under
budget while still finding all three small perfects.
125 baseline programs total — milestone.
A number n is abundant if its proper-divisor sum exceeds n. Reuses
the trial-division div_sum helper:
let count_abundant n =
let c = ref 0 in
for i = 12 to n - 1 do
if div_sum i > i then c := !c + 1
done;
!c
count_abundant 100 = 21
Abundant numbers under 100, starting at 12, 18, 20, 24, 30, 36, 40,
42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96 -> 21.
Companion to euler21_small.ml (amicable). The classification:
perfect: d(n) = n (e.g. 6, 28)
abundant: d(n) > n (e.g. 12, 18)
deficient:d(n) < n (everything else)
124 baseline programs total.
Numbers that read the same in base 10 and base 2:
1, 3, 5, 7, 9, 33, 99, 313, 585, 717
sum = 1772
Implementation:
pal_dec n check decimal palindrome via index walk
to_binary n build binary string via mod 2 / div 2 stack
pal_bin n check binary palindrome
euler36 limit scan 1..limit-1, sum where both palindromes
Real PE36 uses 10^6 (answer 872187). 1000 takes ~9 minutes on
contended host but stays within reasonable budget for the
spec-level evaluator.
123 baseline programs total.
Build the Champernowne string '12345678910111213...' until at
least 1500 chars; product of digits at positions 1, 10, 100, 1000
is 1 * 1 * 5 * 3 = 15.
Initial implementation timed out: 'String.length (Buffer.contents
buf) < 1500' rebuilt the full string each iteration (O(n^2) in our
spec-level evaluator). Fixed by tracking length separately from
the Buffer:
let len = ref 0 in
while !len < 1500 do
let s = string_of_int !i in
Buffer.add_string buf s;
len := !len + String.length s;
i := !i + 1
done
Real PE40 uses positions up to 10^6 (answer 210); 1000 keeps under
budget while exercising the same string-build + char-pick pattern.
122 baseline programs total.
Number that equals the sum of factorials of its digits:
145 = 1! + 4! + 5! = 1 + 24 + 120
Implementation:
fact n iterative factorial
digit_fact_sum n walk digits, sum fact(digit)
euler34 limit scan 3..limit, accumulate matches
The only other factorion is 40585 = 4!+0!+5!+8!+5!. Real PE34 sums
both (= 40730); 2000 keeps under our search budget.
121 baseline programs total.
Compute every power a^b for a, b in [2..N] and count distinct
values. Hashtbl as a set with unit-payload (iter-168 idiom):
let euler29 n =
let h = Hashtbl.create 64 in
for a = 2 to n do
for b = 2 to n do
let p = ref 1 in
for _ = 1 to b do p := !p * a done;
Hashtbl.replace h !p ()
done
done;
Hashtbl.length h
For N=5: 16 powers minus one duplicate (4^2 = 2^4 = 16) -> 15.
Real PE29 uses N=100 (answer 9183).
120 baseline programs total — milestone.
div_sum computes proper divisor sum via trial division up to sqrt(n):
let div_sum n =
let s = ref 1 in
let i = ref 2 in
while !i * !i <= n do
if n mod !i = 0 then begin
s := !s + !i;
let q = n / !i in
if q <> !i then s := !s + q
end;
i := !i + 1
done;
if n = 1 then 0 else !s
Outer loop finds amicable pairs (a, b) with d(a) = b, d(b) = a,
a != b. Only pair under 300 is (220, 284); 220 + 284 = 504.
Real PE21 uses 10000 (answer 31626). 300 keeps the run under
budget while exercising the same divisor-sum trick.
119 baseline programs total.
Numbers equal to the sum of cubes of their digits:
153 = 1 + 125 + 27
370 = 27 + 343 + 0
371 = 27 + 343 + 1
407 = 64 + 0 + 343
sum = 1301
Implementation:
pow_digit_sum n p walk digits of n, accumulate d^p
euler30 p limit scan 2..limit and sum where pow_digit_sum n p = n
Real PE30 uses 5th powers (answer 443839); the cube version
exercises the same algorithm in a smaller search space.
118 baseline programs total.
For each layer 1..(n-1)/2, the four corners of an Ulam spiral are
spaced 2*layer apart. Step k four times per layer, accumulate:
let euler28 n =
let s = ref 1 in
let k = ref 1 in
for layer = 1 to (n - 1) / 2 do
let step = 2 * layer in
for _ = 1 to 4 do
k := !k + step;
s := !s + !k
done
done;
!s
euler28 7 = 1 + (3+5+7+9) + (13+17+21+25) + (31+37+43+49) = 261
Real PE28 uses 1001x1001 (answer 669171001); 7x7 is fast.
117 baseline programs total.
collatz_len walks n through n/2 if even, 3n+1 if odd, counting
steps. Outer loop scans 2..N tracking the best length and arg-best:
let euler14 limit =
let best = ref 0 in
let best_n = ref 0 in
for n = 2 to limit do
let l = collatz_len n in
if l > !best then begin
best := l;
best_n := n
end
done;
!best_n
euler14 100 = 97 (97 generates a 118-step chain)
Real PE14 uses limit = 1_000_000 (answer 837799); 100 exercises the
same algorithm in <2 minutes on our contended host.
116 baseline programs total.
Computes 2^n via for-loop multiplication, then walks the digits via
mod 10 / div 10:
let euler16 n =
let p = ref 1 in
for _ = 1 to n do p := !p * 2 done;
let sum = ref 0 in
let m = ref !p in
while !m > 0 do
sum := !sum + !m mod 10;
m := !m / 10
done;
!sum
euler16 15 = 3 + 2 + 7 + 6 + 8 = 26 (= digit sum of 32768)
Real PE16 asks for 2^1000 which exceeds float precision; 2^15 stays
safe and exercises the same digit-decomposition pattern.
115 baseline programs total.
Iteratively grows two refs while the larger is below 10^(n-1),
counting iterations:
let euler25 n =
let a = ref 1 in
let b = ref 1 in
let i = ref 2 in
let target = ref 1 in
for _ = 1 to n - 1 do target := !target * 10 done;
while !b < !target do
let c = !a + !b in
a := !b;
b := c;
i := !i + 1
done;
!i
euler25 12 = 55 (F(55) = 139_583_862_445, 12 digits)
Real PE25 asks for 1000 digits (answer 4782); 12 keeps within
safe-int while exercising the identical algorithm.
114 baseline programs total — 200 iterations landed.
PE3's worked example. Trial-division streaming: when the current
factor divides m, divide and update largest; otherwise bump factor:
let largest_prime_factor n =
let m = ref n in
let factor = ref 2 in
let largest = ref 0 in
while !m > 1 do
if !m mod !factor = 0 then begin
largest := !factor;
m := !m / !factor
end else factor := !factor + 1
done;
!largest
largest_prime_factor 13195 = 29 (= 5 * 7 * 13 * 29)
The full PE3 number 600851475143 exceeds JS safe-int (2^53 ≈ 9e15
in float terms; 6e11 is fine but the intermediate 'i mod !factor'
on the way to 6857 can overflow precision). 13195 keeps the program
portable across hosts.
113 baseline programs total.
Scaled-down PE7 (real version asks for the 10001st prime = 104743).
Trial-division within an outer while loop searching forward from 2,
short-circuited via bool ref:
let nth_prime n =
let count = ref 0 in
let i = ref 1 in
let result = ref 0 in
while !count < n do
i := !i + 1;
let p = ref true in
let j = ref 2 in
while !j * !j <= !i && !p do
if !i mod !j = 0 then p := false;
j := !j + 1
done;
if !p then begin
count := !count + 1;
if !count = n then result := !i
end
done;
!result
nth_prime 100 = 541
100 keeps the run under our 3-minute budget while exercising the
same algorithm.
112 baseline programs total.
Scaled-down Project Euler #4. Real version uses 3-digit numbers
yielding 906609 = 913 * 993; that's an 810k-iteration nested loop
that times out under our contended-host spec-level evaluator.
The 2-digit version (10..99) is fast enough and tests the same
algorithm:
9009 = 91 * 99 (the only 2-digit-product palindrome > 9000)
Implementation:
is_pal n index-walk comparing s.[i] to s.[len-1-i]
euler4 lo hi nested for with running max + early-skip via
'p > !m && is_pal p' short-circuit
111 baseline programs total.
Sieve of Eratosthenes followed by a sum loop:
let sieve_sum n =
let s = Array.make (n + 1) true in
s.(0) <- false;
s.(1) <- false;
for i = 2 to n do
if s.(i) then begin
let j = ref (i * i) in
while !j <= n do
s.(!j) <- false;
j := !j + i
done
end
done;
let total = ref 0 in
for i = 2 to n do
if s.(i) then total := !total + i
done;
!total
Real PE10 asks for sum below 2,000,000; that's a ~2-3 second loop in
native OCaml but minutes-to-hours under our contended-host
spec-level evaluator. 100 keeps the run under 3 minutes while still
exercising the same algorithm.
110 baseline programs total.
Iteratively takes lcm of running result with i:
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let lcm a b = a * b / gcd a b
let euler5 n =
let r = ref 1 in
for i = 2 to n do
r := lcm !r i
done;
!r
euler5 20 = 232792560
= 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19
Tests gcd_lcm composition (iter 140) on a fresh problem.
109 baseline programs total.
Two ref lists accumulating in reverse, then List.rev'd — preserves
original order:
let partition pred xs =
let yes = ref [] in
let no = ref [] in
List.iter (fun x ->
if pred x then yes := x :: !yes
else no := x :: !no
) xs;
(List.rev !yes, List.rev !no)
partition (fun x -> x mod 2 = 0) [1..10]
-> ([2;4;6;8;10], [1;3;5;7;9])
evens sum * 100 + odds sum = 30 * 100 + 25 = 3025
Tests higher-order predicate, tuple return, and iter-98 let-tuple
destructuring on the call site.
108 baseline programs total.
Trial division up to sqrt(n) with early-exit via bool ref:
let is_prime n =
if n < 2 then false
else
let p = ref true in
let i = ref 2 in
while !i * !i <= n && !p do
if n mod !i = 0 then p := false;
i := !i + 1
done;
!p
Outer count_primes loops 2..n calling is_prime, accumulating count.
Returns 25 — the canonical prime-counting function pi(100).
107 baseline programs total.
DP recurrence:
C(0) = 1
C(n) = sum_{j=0}^{n-1} C(j) * C(n-1-j)
let catalan n =
let dp = Array.make (n + 1) 0 in
dp.(0) <- 1;
for i = 1 to n do
for j = 0 to i - 1 do
dp.(i) <- dp.(i) + dp.(j) * dp.(i - 1 - j)
done
done;
dp.(n)
C(5) = 42 — also the count of distinct binary trees with 5 internal
nodes, balanced paren strings of length 10, monotonic lattice paths,
etc.
106 baseline programs total.
Two functions:
classify n maps i to a polymorphic variant
FizzBuzz | Fizz | Buzz | Num of int
score x pattern-matches the variant to a weight
FizzBuzz=100, Fizz=10, Buzz=5, Num n=n
For i in 1..30:
FizzBuzz at 15, 30: 2 * 100 = 200
Fizz at 3,6,9,12,18,21,24,27: 8 * 10 = 80
Buzz at 5,10,20,25: 4 * 5 = 20
Num: rest (16 numbers) = 240
total = 540
Exercises polymorphic-variant match (iter 87) including a
payload-bearing 'Num n' arm.
105 baseline programs total.
Sort, then compare two candidates:
p1 = product of three largest values
p2 = product of two smallest (potentially negative) values and the largest
For [-10;-10;1;3;2]:
sorted = [-10;-10;1;2;3]
p1 = 3 * 2 * 1 = 6
p2 = (-10) * (-10) * 3 = 300
max = 300
Tests List.sort + Array.of_list + arr.(n-i) end-walk + candidate-pick
via if-then-else.
104 baseline programs total.
Find the unique Pythagorean triple with a + b + c = 1000 and
return their product.
The naive triple loop timed out under host contention (10-minute
cap exceeded with ~333 * 999 ~= 333k inner iterations of complex
checks). Rewritten with algebraic reduction:
a + b + c = 1000 AND a^2 + b^2 = c^2
=> b = (500000 - 1000 * a) / (1000 - a)
so only the outer a-loop is needed (333 iterations). Single-pass
form:
for a = 1 to 333 do
let num = 500000 - 1000 * a in
let den = 1000 - a in
if num mod den = 0 then begin
let b = num / den in
if b > a then
let c = 1000 - a - b in
if c > b then result := a * b * c
end
done
Triple (200, 375, 425), product 31875000.
103 baseline programs total.
Project Euler #6: difference between square of sum and sum of squares
for 1..100.
let euler6 n =
let sum = ref 0 in
let sum_sq = ref 0 in
for i = 1 to n do
sum := !sum + i;
sum_sq := !sum_sq + i * i
done;
!sum * !sum - !sum_sq
euler6 100 = 5050^2 - 338350 = 25502500 - 338350 = 25164150
102 baseline programs total.
Sum of even-valued Fibonacci numbers up to 4,000,000:
let euler2 limit =
let a = ref 1 in
let b = ref 2 in
let sum = ref 0 in
while !a <= limit do
if !a mod 2 = 0 then sum := !sum + !a;
let c = !a + !b in
a := !b;
b := c
done;
!sum
Sequence: 1, 2, 3, 5, 8, 13, 21, 34, ... Only every third term
(2, 8, 34, 144, ...) is even. Sum below 4M: 4613732.
101 baseline programs total.
Project Euler #1: sum of all multiples of 3 or 5 below 1000.
let euler1 limit =
let sum = ref 0 in
for i = 1 to limit - 1 do
if i mod 3 = 0 || i mod 5 = 0 then sum := !sum + i
done;
!sum
euler1 1000 = 233168
Trivial DSL exercise but symbolically meaningful: this is the 100th
baseline program.
100 baseline programs total — milestone.
canonical builds a sorted-by-frequency string representation:
let canonical s =
let chars = Array.make 26 0 in
for i = 0 to String.length s - 1 do
let k = Char.code s.[i] - Char.code 'a' in
if k >= 0 && k < 26 then chars.(k) <- chars.(k) + 1
done;
expand into a-z order via a Buffer
For 'eat', 'tea', 'ate' -> all canonicalise to 'aet'. For 'tan',
'nat' -> 'ant'. For 'bat' -> 'abt'.
group_anagrams folds the input, accumulating per-key string lists;
final answer is Hashtbl.length (number of distinct groups):
['eat'; 'tea'; 'tan'; 'ate'; 'nat'; 'bat'] -> 3 groups
99 baseline programs total.
Tracks two bool refs (inc, dec). For each pair of consecutive
elements: if h < prev clear inc, if h > prev clear dec. Returns
inc OR dec at the end:
let is_monotonic xs =
match xs with
| [] -> true
| [_] -> true
| _ ->
let inc = ref true in
let dec = ref true in
let rec walk prev rest = ... in
(match xs with h :: t -> walk h t | [] -> ());
!inc || !dec
Five test cases:
[1;2;3;4] inc only true
[4;3;2;1] dec only true
[1;2;1] neither false
[5;5;5] both (constant) true
[] empty true (vacuous)
sum = 4
98 baseline programs total.
O(n) time / O(1) space majority vote algorithm:
let majority xs =
let cand = ref 0 in
let count = ref 0 in
List.iter (fun x ->
if !count = 0 then begin
cand := x;
count := 1
end else if x = !cand then count := !count + 1
else count := !count - 1
) xs;
!cand
The candidate is updated to the current element whenever count
reaches zero. When a strict majority exists, this guarantees the
result.
majority [3;3;4;2;4;4;2;4;4] = 4 (5 of 9, > n/2)
97 baseline programs total.
Two running sums modulo 65521:
a = (1 + sum of bytes) mod 65521
b = sum of running 'a' values mod 65521
checksum = b * 65536 + a
let adler32 s =
let a = ref 1 in
let b = ref 0 in
let m = 65521 in
for i = 0 to String.length s - 1 do
a := (!a + Char.code s.[i]) mod m;
b := (!b + !a) mod m
done;
!b * 65536 + !a
For 'Wikipedia': 0x11E60398 = 300286872 (the canonical test value).
Tests for-loop accumulating two refs together, modular arithmetic,
and Char.code on s.[i].
96 baseline programs total.
Single-formula generation:
gray[i] = i lxor (i lsr 1)
For n = 4, generates 16 values, each differing from its neighbour
by one bit. Output is a permutation of 0..15, so its sum equals the
natural-sequence sum 120; +16 entries -> 136.
Tests lsl / lxor / lsr together (the iter-127 bitwise ops) plus
Array.make / Array.fold_left.
95 baseline programs total.
Walks list keeping a previous-value reference; increments cur on
match, resets to 1 otherwise. Uses 'Some y when y = x' guard pattern
in match for the prev-value comparison:
let max_run xs =
let max_so_far = ref 0 in
let cur = ref 0 in
let last = ref None in
List.iter (fun x ->
(match !last with
| Some y when y = x -> cur := !cur + 1
| _ -> cur := 1);
last := Some x;
if !cur > !max_so_far then max_so_far := !cur
) xs;
!max_so_far
Three test cases:
[1;1;2;2;2;2;3;3;1;1;1] max run = 4 (the 2's)
[1;2;3;4;5] max run = 1
[] max run = 0
sum = 5
Tests 'when' guard pattern in match arm + Option ref + ref-mutation
sequence inside List.iter closure body.
94 baseline programs total.
Two-pointer walk:
let is_subseq s t =
let i = ref 0 in
let j = ref 0 in
while !i < n && !j < m do
if s.[!i] = t.[!j] then i := !i + 1;
j := !j + 1
done;
!i = n
advance i only on match; always advance j. Pattern matches if i
reaches n.
Five test cases:
'abc' in 'ahbgdc' yes
'axc' in 'ahbgdc' no (no x in t)
'' in 'anything' yes (empty trivially)
'abc' in 'abc' yes
'abcd' in 'abc' no (s longer)
sum = 3
93 baseline programs total.
Sort intervals by start, then sweep maintaining a current (cs, ce)
window — extend ce if next start <= ce, else push current and start
fresh:
let merge_intervals xs =
let sorted = List.sort (fun (a, _) (b, _) -> a - b) xs in
let rec aux acc cur xs =
match xs with
| [] -> List.rev (cur :: acc)
| (s, e) :: rest ->
let (cs, ce) = cur in
if s <= ce then aux acc (cs, max e ce) rest
else aux (cur :: acc) (s, e) rest
in
match sorted with
| [] -> []
| h :: rest -> aux [] h rest
[(1,3);(2,6);(8,10);(15,18);(5,9)]
-> [(1,10); (15,18)]
total length = 9 + 3 = 12
Tests List.sort with custom comparator using tuple patterns, plus
tuple destructuring in lambda + let-tuple from accumulator + match
arms.
92 baseline programs total.