Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] String.map => Question to the OCaml-team
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Christophe Filliatre <filliatr@l...>
Subject: Re: [Caml-list] String.map => Question to the OCaml-team

 > I don't know much about ocaml, but my bet is the implementation of
 > strings doens't allow for anything considerably more efficient than
 > this:
 > 
 > let string_map (f: char -> char) (s: string) : string =
 >   let t = String.copy s in
 >   let i = ref 0 in
 >   String.iter (fun c -> let d = f c in String.set t !i d; incr i) s;
 >   t;;

Note there is a `for' construct in ocaml:

======================================================================
let string_map (f: char -> char) (s: string) : string =
  let t = String.copy s in
  for i = 0 to String.length s - 1 do t.[i] <- f s.[i] done;
  t
======================================================================

and this is slightly faster than your implementation (by 10%).

-- 
Jean-Christophe

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners