From 836e01dbb439c312f0eb7873d4a1551acac7e57c Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 01:39:25 +0000 Subject: [PATCH] ocaml: phase 5.1 number_words.ml baseline (letter count of 1..19 spelled out = 106) 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. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/number_words.ml | 19 +++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 26 insertions(+) create mode 100644 lib/ocaml/baseline/number_words.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index bb8e8c68..f8367cde 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -86,6 +86,7 @@ "module_use.ml": 3, "monotonic.ml": 4, "newton_sqrt.ml": 1414, + "number_words.ml": 106, "mutable_record.ml": 10, "option_match.ml": 5, "palindrome.ml": 4, diff --git a/lib/ocaml/baseline/number_words.ml b/lib/ocaml/baseline/number_words.ml new file mode 100644 index 00000000..467b5eaa --- /dev/null +++ b/lib/ocaml/baseline/number_words.ml @@ -0,0 +1,19 @@ +let number_to_words n = + match n with + | 1 -> "one" | 2 -> "two" | 3 -> "three" | 4 -> "four" | 5 -> "five" + | 6 -> "six" | 7 -> "seven" | 8 -> "eight" | 9 -> "nine" + | 10 -> "ten" | 11 -> "eleven" | 12 -> "twelve" + | 13 -> "thirteen" | 14 -> "fourteen" | 15 -> "fifteen" + | 16 -> "sixteen" | 17 -> "seventeen" | 18 -> "eighteen" | 19 -> "nineteen" + | _ -> "" + +let total_letters limit = + let total = ref 0 in + for i = 1 to limit do + total := !total + String.length (number_to_words i) + done; + !total + +;; + +total_letters 19 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 64e0a2e2..fee3ee91 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _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 — 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 + PE17 covers 1..1000 (answer 21124) but requires more elaborate + number-to-words logic. Tests literal-pattern match with many arms. + 128 baseline programs total. - 2026-05-10 Phase 5.1 — palindrome_sum.ml baseline (sum of 3-digit palindromes = 49500). 90 palindromes between 100 and 999 (form aba; 9 choices for a, 10 for b). Sum = 49500 = 90 * 550 (mean