let set s ~index ~v = let rec loop n acc = function | [] -> None | q :: t when n = index -> Some (List.rev_append acc (v :: t)) | q :: t -> loop (n + 1) (q :: acc) t in loop 0 [] s