Files
rose-ash/lib/ocaml/baseline/caesar.ml
giles 0234ae329e
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
ocaml: phase 5.1 caesar.ml baseline (ROT13 + s.[i] + Char ops)
Side-quests required to land caesar.ml:

1. Top-level 'let r = expr in body' is now an expression decl, not a
   broken decl-let. ocaml-parse-program's dispatch now checks
   has-matching-in? at every top-level let; if matched, slices via
   skip-let-rhs-boundary (which already opens depth on a leading let
   with matching in) and ocaml-parse on the slice, wrapping as :expr.

2. runtime.sx: added String.make / String.init / String.map. Used by
   caesar.ml's encode = String.init n (fun i -> shift_char s.[i] k).

3. baseline run.sh per-program timeout 240->480s (system load on the
   shared host frequently exceeds 240s for large baselines).

caesar.ml exercises:
  * the new top-level let-in expression dispatch
  * s.[i] string indexing
  * Char.code / Char.chr round-trip math
  * String.init with a closure that captures k

Test value: Char.code r.[0] + Char.code r.[4] after ROT13(ROT13('hello')) = 104 + 111 = 215.
2026-05-09 00:13:11 +00:00

15 lines
432 B
OCaml

let shift_char c k =
let n = Char.code c in
if n >= 97 && n <= 122 then
Char.chr (((n - 97 + k) mod 26 + 26) mod 26 + 97)
else c
let encode s k =
String.init (String.length s) (fun i -> shift_char s.[i] k)
;;
(* ROT13 round-trip: encode (encode "hello" 13) 13 = "hello".
Sum the codes of two chars to give a deterministic integer check. *)
let r = encode (encode "hello" 13) 13 in
Char.code r.[0] + Char.code r.[4]