caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] String.map => Question to the OCaml-team
@ 2004-04-09 11:01 Oliver Bandel
  2004-04-09 12:26 ` fis
  2004-04-09 12:28 ` Jon Harrop
  0 siblings, 2 replies; 10+ messages in thread
From: Oliver Bandel @ 2004-04-09 11:01 UTC (permalink / raw)
  To: caml-list

Hello,


it's nice to have a String.iter, but more functional would
be to have a String.map function.

Will this maybe added to the standard lib?

Ciao,
   Oliver

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 11:01 [Caml-list] String.map => Question to the OCaml-team Oliver Bandel
@ 2004-04-09 12:26 ` fis
  2004-04-09 13:29   ` Jean-Christophe Filliatre
  2004-04-09 12:28 ` Jon Harrop
  1 sibling, 1 reply; 10+ messages in thread
From: fis @ 2004-04-09 12:26 UTC (permalink / raw)
  To: caml-list



> it's nice to have a String.iter, but more functional would
> be to have a String.map function.

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;;

to test, type:

let s = "a=b1";;
let t = string_map (fun c -> char_of_int (int_of_char c + 1)) s;;

Does this generate optimal code, or where not?  What are the technical
/ political procedures to put these lines into the next distribution?
Are there any good reasons against doing so?

Well, I guess there always are...  (-:

curious,
matthias

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 11:01 [Caml-list] String.map => Question to the OCaml-team Oliver Bandel
  2004-04-09 12:26 ` fis
@ 2004-04-09 12:28 ` Jon Harrop
  2004-04-10 10:14   ` skaller
  1 sibling, 1 reply; 10+ messages in thread
From: Jon Harrop @ 2004-04-09 12:28 UTC (permalink / raw)
  To: caml-list

On Friday 09 April 2004 12:01 pm, Oliver Bandel wrote:
> it's nice to have a String.iter, but more functional would
> be to have a String.map function.

Would that be:

val map : (char -> char) -> string -> string

or

val map : (char -> 'a) -> string -> 'a list

?

But yes, ironically, now that I return to coding I need exactly this function 
(the latter, actually). :)

Cheers,
Jon.

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 12:26 ` fis
@ 2004-04-09 13:29   ` Jean-Christophe Filliatre
  2004-04-09 14:44     ` fis
  2004-04-09 14:56     ` Fernando Alegre
  0 siblings, 2 replies; 10+ messages in thread
From: Jean-Christophe Filliatre @ 2004-04-09 13:29 UTC (permalink / raw)
  To: fis, Oliver Bandel; +Cc: caml-list


 > 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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 13:29   ` Jean-Christophe Filliatre
@ 2004-04-09 14:44     ` fis
  2004-04-09 15:12       ` Correnson Loïc
  2004-04-09 14:56     ` Fernando Alegre
  1 sibling, 1 reply; 10+ messages in thread
From: fis @ 2004-04-09 14:44 UTC (permalink / raw)
  To: caml-list



Jean-Christophe Filliatre writes:
> From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
> Date: Fri, 9 Apr 2004 15:29:41 +0200
> 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:

I dislike loops...  (-:

But I am an idiot.  In this case, a loop is not only closer to how a
CPU thinks but also leads to a more elegant representation of the
algorithm.

I assume that one reason why the loop is faster is the additional
reference cell access operations in my code.  (I still think there is
some way of compiling away the reference in this particular example
using very general compiler patterns.  Not sure, though.  Perhaps I am
still an idiot.)

matthias

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 13:29   ` Jean-Christophe Filliatre
  2004-04-09 14:44     ` fis
@ 2004-04-09 14:56     ` Fernando Alegre
  2004-04-09 17:00       ` brogoff
  1 sibling, 1 reply; 10+ messages in thread
From: Fernando Alegre @ 2004-04-09 14:56 UTC (permalink / raw)
  To: Jean-Christophe Filliatre; +Cc: fis, Oliver Bandel, caml-list

On Fri, Apr 09, 2004 at 03:29:41PM +0200, Jean-Christophe Filliatre wrote:

> 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%).

Why not just use String.blit, as provided by the standard library?

Fernando

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 14:44     ` fis
@ 2004-04-09 15:12       ` Correnson Loïc
  0 siblings, 0 replies; 10+ messages in thread
From: Correnson Loïc @ 2004-04-09 15:12 UTC (permalink / raw)
  To: fis; +Cc: Ocaml

Have also a look at Array.iteri :
It allows to not use a for, nor an extra ref.
It shall be possible to have in lib:

String.iter : (char -> unit) -> string -> unit
String.iteri : (int ->char -> unit) -> string -> unit

Then:
let map f s = let t = String.copy s in ( String.iteri (fun i c -> t.[i] <- f
c) s ; t )

However, without extra libs nor for, but with a tail-rec function :
let map f s =
    let rec iteri i n t = if i<n then (t.[i] <- f t.[i] ; iteri (succ i) n
t) else t in
    iteri 0 (String.length s) (String.copy s)

Don't known what about performances.
    LC.

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 14:56     ` Fernando Alegre
@ 2004-04-09 17:00       ` brogoff
  2004-04-09 18:26         ` Nicolas Cannasse
  0 siblings, 1 reply; 10+ messages in thread
From: brogoff @ 2004-04-09 17:00 UTC (permalink / raw)
  To: caml-list

On Fri, 9 Apr 2004, Fernando Alegre wrote:

> On Fri, Apr 09, 2004 at 03:29:41PM +0200, Jean-Christophe Filliatre wrote:
>
> > 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%).
>
> Why not just use String.blit, as provided by the standard library?
>
> Fernando
>
The whole thing would be a lot nicer if there were a String.init

    init : int -> (int -> char) -> string

obviously modeled on Array.init, since the OCaml designers had enough good
sense not to follow Haskell and make strings lists (or provide abominable
built in string -> char list functions). I wonder how usefulthis is in
typical string  manipulation? I guess I find other ways to do things...

It would of course be nice if we had the oft discussed generic polymorphism
so that we could take better advantage of the similarity of strings and arrays
(and bigarrays and hastables and ...) than we can now. Bazaar to Cathedral,
anyone home? :-)

Also, ripping off SML97 Basis Substring makes a lot of sense. Substrings allow
you to write these string traversal algorithms over in a functional style,
which is usually a good thing.

-- Brian

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 17:00       ` brogoff
@ 2004-04-09 18:26         ` Nicolas Cannasse
  0 siblings, 0 replies; 10+ messages in thread
From: Nicolas Cannasse @ 2004-04-09 18:26 UTC (permalink / raw)
  To: brogoff, caml-list

> > > 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%).
> >
> > Why not just use String.blit, as provided by the standard library?
> >
> > Fernando
> >
> The whole thing would be a lot nicer if there were a String.init
>
>     init : int -> (int -> char) -> string
>
> obviously modeled on Array.init, since the OCaml designers had enough good
> sense not to follow Haskell and make strings lists (or provide abominable
> built in string -> char list functions). I wonder how usefulthis is in
> typical string  manipulation? I guess I find other ways to do things...
>
> It would of course be nice if we had the oft discussed generic
polymorphism
> so that we could take better advantage of the similarity of strings and
arrays
> (and bigarrays and hastables and ...) than we can now. Bazaar to
Cathedral,
> anyone home? :-)

Look at ExtLib :-)
we added String.init and we have Enum module which can play with String as
Array of chars without actually allocating an array of chars.
http://ocaml-lib.sf.net
Of course generics would add more sugar into our everyday caml coffee.

Regards,
Nicolas Cannasse

-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] String.map => Question to the OCaml-team
  2004-04-09 12:28 ` Jon Harrop
@ 2004-04-10 10:14   ` skaller
  0 siblings, 0 replies; 10+ messages in thread
From: skaller @ 2004-04-10 10:14 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Fri, 2004-04-09 at 22:28, Jon Harrop wrote:
> On Friday 09 April 2004 12:01 pm, Oliver Bandel wrote:
> > it's nice to have a String.iter, but more functional would
> > be to have a String.map function.
> 
> Would that be:
> 
> val map : (char -> char) -> string -> string
> 
> or
> 
> val map : (char -> 'a) -> string -> 'a list
> 
> ?
> 
> But yes, ironically, now that I return to coding I need exactly this function 
> (the latter, actually). :)

And I need:

val map: (char -> string) -> string -> string

which is by far the most common need. An example is:

map to_UTF8 latin1_string

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-04-10 10:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-09 11:01 [Caml-list] String.map => Question to the OCaml-team Oliver Bandel
2004-04-09 12:26 ` fis
2004-04-09 13:29   ` Jean-Christophe Filliatre
2004-04-09 14:44     ` fis
2004-04-09 15:12       ` Correnson Loïc
2004-04-09 14:56     ` Fernando Alegre
2004-04-09 17:00       ` brogoff
2004-04-09 18:26         ` Nicolas Cannasse
2004-04-09 12:28 ` Jon Harrop
2004-04-10 10:14   ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).