Repeat a string: Difference between revisions

m
Line 1,686:
Since Ocaml 4.02 strings are immutable, as is convenient for a functional language. Mutable strings are now implemented in the module Bytes.
<lang ocaml>let string_repeat s n =
let s = Bytes.of_string s in
let len = Bytes.length s in
let res = Bytes.create (n * len) in
for i = 0 to pred n do
Bytes.blit s 0 res (i * len) len
done;
(Bytes.to_string res (* not stricly necessary, the bytes type is equivalent to string except mutability *)
;;</lang>
which gives the signature<lang ocaml> val string_repeat : bytesstring -> int -> string = <fun></lang>
 
testing in the toplevel: