I've been trying to figure out how to resize an already initialized array in OCaml. However, it seems that while you can write a function that will create a brand new array with the elements of the old one copied over (and extra slots), that function's output cannot be assigned to existing array. How would one do this? Is there an easy way in which to use references to make this happen if not without?
Here's a tiny example:
let rec function_that_adds_to_array storage args ... =
(* let's say the function has a set of if-else constructs that controls what it does, and let's further say that one of the cases leads to: *)
let new_array = Array.make (Array.length storage) ("Null", ("Null", -2)) in
Array.blit collection 0 new_array 0 index; (* index is controlled by the function's recursion *)
Array.set new_array index (obj_name, obj_expr);
new_array) (* insert the macro at tail *)
...
;;
### main method ###
let storage = Array.make 10 ((x : string), (a, b)) in
...
while true do
...
storage = function_that_adds_to_array storage args....;
...
A print statement at the end of the function_that_adds_to_array(...) confirms that a new array is returned, containing the old elements of the initial array, however, in the main method, storage remains identical. Is this because of the immutability of OCaml's elements? I thought Arrays were mutable. I've looked around, and some mention writing hacks to getting OCaml to act like Perl, however, using one individual's resize hack function proved futile. Any way I can get storage to become a new array? It needs to be an update-able collection of tuples (i.e. (string, (x, y)) )?
opam install batteries.#require "batteries;;"followed byopen Batteries;;- rgrinberg