Sorry, I should have made clear that this is not a problem I wanted solved for me, but rather a question about OCaml. I've just never come across this before because I don't usually mutate strings.
Observe the following:
# let foo () =
let bar = [|'a';'b';'c'|] in
Array.iter (Printf.printf "%c") bar;
bar.(0) <- 'd';
bar;;
val foo : unit -> char array = <fun>
# foo ();;
abc- : char array = [|'d'; 'b'; 'c'|]
# foo ();;
abc- : char array = [|'d'; 'b'; 'c'|]
Why does OCaml treat these two examples in such a different manner? Is there a reason why strings are magically special in this way?
Each invocation of foo does not allocate a new string for str, "ffff"
is a constant string allocated once and you are updating this constant.
let str = String.copy "ffff"
will solve your problem.
Best,
Daniel