OCaml deep clone and recursive merge for config trees

OCaml
type tree = Leaf of string | Node of (string * tree) list

(* Strings are immutable, so only nodes need copying. *)
let rec clone = function
  | Leaf s -> Leaf s
  | Node fields -> Node (List.map (fun (k, v) -> (k, clone v)) fields)

let rec merge a b =
  match (a, b) with
  | Node xs, Node ys ->
    List.fold_left (fun acc (k, v) ->
      let merged =
        match List.assoc_opt k acc with
        | Some old -> merge old v
        | None -> clone v
      in
      (k, merged) :: List.remove_assoc k acc) xs ys
  | _, other -> clone other