diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index b076242f..1b08ac96 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -16,6 +16,7 @@ "frequency.ml": 5, "fizzbuzz.ml": 57, "list_ops.ml": 30, + "json_pretty.ml": 24, "lambda_calc.ml": 7, "levenshtein.ml": 11, "memo_fib.ml": 75025, diff --git a/lib/ocaml/baseline/json_pretty.ml b/lib/ocaml/baseline/json_pretty.ml new file mode 100644 index 00000000..861866c8 --- /dev/null +++ b/lib/ocaml/baseline/json_pretty.ml @@ -0,0 +1,20 @@ +type json = + | JNull + | JBool of bool + | JInt of int + | JStr of string + | JList of json list + +let rec to_string j = + match j with + | JNull -> "null" + | JBool b -> if b then "true" else "false" + | JInt n -> string_of_int n + | JStr s -> "\"" ^ s ^ "\"" + | JList xs -> + "[" ^ String.concat "," (List.map to_string xs) ^ "]" + +;; + +let j = JList [JInt 1; JBool true; JNull; JStr "hi"; JList [JInt 2; JInt 3]] in +String.length (to_string j) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 7ac4126c..840fbdc9 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _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 — json_pretty.ml baseline (recursive ADT + serialization). Defines a JSON-like ADT (JNull / JBool / JInt / + JStr / JList) and recursively pretty-prints to a string, then + measures length. Tests algebraic data types with five constructors + (one nullary, three single-arg, one list-arg), recursive `match` + with five arms, `String.concat "," (List.map ...)`, and string + concatenation. `[1,true,null,"hi",[2,3]]` → 24 chars. 34 baseline + programs total. - 2026-05-09 Phase 5.1 — shuffle.ml baseline (Fisher-Yates with deterministic Random.init seed). In-place swap loop using `for i = n - 1 downto 1` and `a.(i) <- a.(j)`. Sum is invariant under