Make many insertions in long string

"[email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>
 I have a string of  716954 characters, and I need to make
10998 insertions of short strings at different places in it. 
My naive try (see below)
makes my Mac crash (everything stalls and I'm forced to restart
my computer). Any ideas on how to handle this "massive" computation ?

Perhaps I should take advantage of the fact that I already know
the length of the final answer string.

let beginning k s=
   if k<1 then "" else
   let n=String.length(s) in
   if (k>n)
   then failwith("Beginning failure : string too short")
   else String.sub s 0 k;;
   
 let ending k s=
   if k<1 then "" else
   let n=String.length(s) in
   if (k>n)
   then failwith("Ending failure : string too short")
   else String.sub s (n-k) k;;
    
 let cobeginning k s=ending (String.length(s)-k) s;; 

let insert_at_point s (j,t)=
     let temp1=beginning j s
     and temp2=cobeginning j s in
     temp1^t^temp2;;
    
let insert_at_points s l=List.fold_left insert_at_point s (List.rev l);;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.