From df6efeb68e84006a17a3c7e1c1fdc812df32be52 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 18:27:50 +0000 Subject: [PATCH] ocaml: phase 5.1 hamming.ml baseline (Hamming distance, 3+2-1 = 4) Counts position-wise differences between two strings of equal length; returns -1 sentinel for length mismatch: let hamming s t = if String.length s <> String.length t then -1 else let d = ref 0 in for i = 0 to String.length s - 1 do if s.[i] <> t.[i] then d := !d + 1 done; !d Three test cases: 'karolin' vs 'kathrin' 3 (positions 2,3,4) '1011101' vs '1001001' 2 (positions 2,4) 'abc' vs 'abcd' -1 (length mismatch) sum 4 91 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/hamming.ml | 13 +++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 lib/ocaml/baseline/hamming.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index b767248a..777c5e41 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -34,6 +34,7 @@ "grid_paths.ml": 210, "group_consec.ml": 53, "hailstone.ml": 111, + "hamming.ml": 4, "hanoi.ml": 1023, "hist.ml": 75, "int_sqrt.ml": 1027, diff --git a/lib/ocaml/baseline/hamming.ml b/lib/ocaml/baseline/hamming.ml new file mode 100644 index 00000000..7d59106f --- /dev/null +++ b/lib/ocaml/baseline/hamming.ml @@ -0,0 +1,13 @@ +let hamming s t = + if String.length s <> String.length t then -1 + else begin + let d = ref 0 in + for i = 0 to String.length s - 1 do + if s.[i] <> t.[i] then d := !d + 1 + done; + !d + end + +;; + +hamming "karolin" "kathrin" + hamming "1011101" "1001001" + hamming "abc" "abcd" diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 3e1b5c6f..6ef1d29a 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -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 — hamming.ml baseline (Hamming distance, + 3 + 2 + (-1) = 4). Counts position-wise differences in equal-length + strings; returns -1 sentinel for length mismatch. + karolin vs kathrin 3 (positions 2,3,4) + 1011101 vs 1001001 2 (positions 2,4) + abc vs abcd -1 (length mismatch) + Sum = 4. 91 baseline programs total. - 2026-05-09 Phase 5.1 — xor_cipher.ml baseline (XOR roll-key encryption, round-trip → 601). For each character, XOR with the corresponding key char (key cycled via `i mod kn`). Encrypts