ocaml: phase 5.1 fraction.ml baseline (rational arithmetic, 4/3 -> num+den=7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s

Defines:

  type frac = { num : int; den : int }
  let rec gcd a b = if b = 0 then a else gcd b (a mod b)
  let make n d =                        (* canonicalise: gcd-reduce and
                                           force den > 0 *)
  let add x y = make (x.num * y.den + y.num * x.den) (x.den * y.den)
  let mul x y = make (x.num * y.num) (x.den * y.den)

Test:
  let r = add (make 1 2) (make 1 3) in     (* 5/6 *)
  let s = mul (make 2 3) (make 3 4) in     (* 1/2 *)
  let t = add r s in                       (* 5/6 + 1/2 = 4/3 *)
  t.num + t.den                            (* = 7 *)

Exercises records, recursive gcd, mod, abs, integer division (the
truncate-toward-zero semantics from iter 94 are essential here —
make would diverge from real OCaml's behaviour with float division).
28 baseline programs total.
This commit is contained in:
2026-05-09 06:05:31 +00:00
parent 2d519461c4
commit 7ca5bfbb70
3 changed files with 28 additions and 0 deletions

View File

@@ -407,6 +407,13 @@ _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 — fraction.ml baseline (rational arithmetic
via record + gcd canonicalization). Defines `type frac = { num;
den }`, `make` that reduces via gcd and forces den > 0, `add` and
`mul` constructors. Computes (1/2 + 1/3) + (2/3 * 3/4) = 4/3, sums
num + den = 7. Exercises records, recursive gcd, `mod`, `abs`,
integer division, and the new `Int.rem`-style truncate-zero
division semantics from iteration 94. 28 baseline programs total.
- 2026-05-09 Phase 6 — Seq module (eager, list-backed) (+4 tests,
576 total). Real OCaml's Seq is lazy (a thunk producing
Cons / Nil); ours is just a list, which is adequate for most