ocaml: phase 5.1 zerosafe.ml baseline (Option-chained safe division, sum = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

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.
This commit is contained in:
2026-05-10 01:49:23 +00:00
parent 836e01dbb4
commit e77a2d3a81
3 changed files with 20 additions and 0 deletions

View File

@@ -118,6 +118,7 @@
"tic_tac_toe.ml": 1,
"word_freq.ml": 8,
"xor_cipher.ml": 601,
"zerosafe.ml": 28,
"zigzag.ml": 55,
"zip_unzip.ml": 1000,
"sieve.ml": 15,

View File

@@ -0,0 +1,14 @@
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
;;
(match safe_chain 100 2 5 with Some x -> x | None -> -1)
+ (match safe_chain 100 0 5 with Some x -> x | None -> -1)
+ (match safe_chain 50 5 0 with Some x -> x | None -> -1)
+ (match safe_chain 1000 10 5 with Some x -> x | None -> -1)

View File

@@ -407,6 +407,11 @@ _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-10 Phase 5.1 — zerosafe.ml baseline (Option-chained safe
division, sum 10 + (-1) + (-1) + 20 = 28). safe_div returns None
on division by zero; safe_chain stitches two divisions, propagating
None on either failure. Tests Option chaining + match-on-result
with sentinel default. 129 baseline programs total.
- 2026-05-10 Phase 5.1 — number_words.ml baseline (letter count of
numbers 1-19 spelled out = 106). 19-arm match dispatch returning
the English word for each number. Sums lengths over 1..19. Real