I want to use the Ocaml array instead of list because the memory footprint for array is less than the list.
I have to store big information in array. I am using Array.append to add a new element.
However, Ocaml documentation for
Array.append v1 v2
returns a fresh array containing the concatenation of the arrays v1
It returns a fresh array without modifying the existing array.
After appending many items in the array, array.ml append function throws Out-of_memory error.
Is there any other alternative to this function? [Or perhaps my understanding of Array module is not good]
Thank you in advance.
It’s hard to give you a sound suggestion because we don’t know what you’re trying to achieve.
If you know or can guess the size of the array in advance, just start with a “big enough” array.
Otherwise you might want to consider libraries that implement “dynamic arrays”.
Actually, I do not know the size of array in advance.
Is there any way to resize the array as and when required?
AFAIK, in OCaml with the standard library you cannot do that (to a certain extent you can use
Array.blit). There are however other libraries that implement dynamic arrays: http://ocaml-batteries-team.github.io/batteries-included/hdoc2/BatDynArray.html
An array that resizes as required is normally referred to as a vector. You may want to search for ‘ocaml vector’.