From: Richard J. <ri...@an...> - 2005-02-10 10:09:28
|
On Wed, Feb 09, 2005 at 10:10:03PM -0400, Juancarlo A?ez wrote: > > Why aren't there functions in the standard library to convert strings to > lists of characters and back? As others have said, these functions are not in the standard library. However, useful functions like these[1] are available in Extlib, which you can find here: http://sourceforge.net/projects/ocaml-lib/ and is also available as a binary package for various platforms such as Debian. It contains important functions such as String.map, String.replace_chars, String.slice, String.starts_with, String.ends_with, and many more. Rich. [1] Although embarrassingly, it appears, not these exact functions, which is why I've CC'd to ocaml-lib-devel list. To ocaml-lib-devel: we should provide implementations of http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html#strings -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com |
From: Richard J. <ri...@an...> - 2005-02-15 10:33:26
|
On Mon, Feb 14, 2005 at 08:16:51PM -0500, Aaron Bohannon wrote: > Instead of adding one of these functions, I would much rather see a=20 > "fold" function on strings in the String module. The Array module has=20 > both "iter" and "fold" functions. Why, then, would the String module=20 > provide an "iter" but no "fold"--in a functional language?? The=20 > addition of a fold function would very often eliminate the need to=20 > convert a string to a char list or to introduce imperative-style=20 > programming into an otherwise purely functional section of code (not to= =20 > mention that writing "char_list_of_string" would become trivial if it=20 > ever were necessary to do so). >=20 > I acknowledge the fact that I can write my own fold function, but I just= =20 > wanted to point out that this seems to be the most logical addition to=20 > the String module. If you can suggest suitable fold_left and fold_right functions, then they can be added to ExtLib. Rich. --=20 Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.c= om |
From: Eric C. C. <ec...@cm...> - 2005-02-15 13:34:15
|
On Tue, Feb 15, 2005 at 10:33:16AM +0000, Richard Jones wrote: > If you can suggest suitable fold_left and fold_right functions, then > they can be added to ExtLib. Here's what I use: val string_fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a val string_fold_right : (char -> 'a -> 'a) -> 'a -> string -> 'a let string_fold_left f init str = let n = String.length str in let rec loop i result = if i = n then result else loop (i + 1) (f result str.[i]) in loop 0 init let string_fold_right f init str = let n = String.length str in let rec loop i result = if i = 0 then result else let i' = i - 1 in loop i' (f str.[i'] result) in loop n init -- Eric C. Cooper e c c @ c m u . e d u |
From: Richard J. <ri...@an...> - 2005-02-17 23:40:59
|
On Tue, Feb 15, 2005 at 08:34:03AM -0500, Eric C. Cooper wrote: > On Tue, Feb 15, 2005 at 10:33:16AM +0000, Richard Jones wrote: > > If you can suggest suitable fold_left and fold_right functions, then > > they can be added to ExtLib. > > Here's what I use: > > val string_fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a > val string_fold_right : (char -> 'a -> 'a) -> 'a -> string -> 'a > > let string_fold_left f init str = > let n = String.length str in > let rec loop i result = > if i = n then result > else loop (i + 1) (f result str.[i]) > in > loop 0 init > > let string_fold_right f init str = > let n = String.length str in > let rec loop i result = > if i = 0 then result > else > let i' = i - 1 in > loop i' (f str.[i'] result) > in > loop n init No one had any objections, so I added these and implode/explode to ExtString. Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com |
From: Nicolas C. <war...@fr...> - 2005-02-10 10:23:11
|
> It contains important functions such as String.map, > String.replace_chars, String.slice, String.starts_with, > String.ends_with, and many more. > > Rich. > > [1] Although embarrassingly, it appears, not these exact functions, > which is why I've CC'd to ocaml-lib-devel list. To ocaml-lib-devel: > we should provide implementations of > http://caml.inria.fr/FAQ/FAQ_EXPERT-eng.html#strings If you mean that we should add string -> char list and char list -> string functions to ExtString well I'm not sure it's good. Representing String as char arrays/list is probably the worst (look at C). The best way is to treat theses caracters uniformaly. And BTW, it's possible to do the following : List.of_enum (String.enum s) String.of_enum (List.enum l) Nicolas |
From: Richard J. <ri...@an...> - 2005-02-10 10:30:19
|
On Thu, Feb 10, 2005 at 11:22:48AM +0100, Nicolas Cannasse wrote: > If you mean that we should add string -> char list and char list -> string > functions to ExtString well I'm not sure it's good. Representing String as > char arrays/list is probably the worst (look at C). The best way is to treat > theses caracters uniformaly. It's not all about performance at all costs though. For me, most of the time I want to get something done, and speed simply doesn't matter - all the programs I'm writing at the moment spend 99.99% of their time waiting for a remote SOAP server to respond. > And BTW, it's possible to do the following : > > List.of_enum (String.enum s) > String.of_enum (List.enum l) Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com |
From: Nicolas C. <war...@fr...> - 2005-02-10 10:42:32
|
> > If you mean that we should add string -> char list and char list -> string > > functions to ExtString well I'm not sure it's good. Representing String as > > char arrays/list is probably the worst (look at C). The best way is to treat > > theses caracters uniformaly. > > It's not all about performance at all costs though. For me, most of > the time I want to get something done, and speed simply doesn't matter > - all the programs I'm writing at the moment spend 99.99% of their > time waiting for a remote SOAP server to respond. I didn't mean about performances, my point was about stability. Once you represent Strings as char arrays you're easily starting doing indexed stuff, and introduce a lot of potentatial bugs for the sake of performances since manipulating strings at an higher level is little more costly but less error prone if you're using the right functions. Nicolas |
From: Bardur A. <lis...@sc...> - 2005-02-10 10:44:21
|
On Thu, Feb 10, 2005 at 10:30:14AM +0000, Richard Jones wrote: > On Thu, Feb 10, 2005 at 11:22:48AM +0100, Nicolas Cannasse wrote: > > If you mean that we should add string -> char list and char list -> string > > functions to ExtString well I'm not sure it's good. Representing String as > > char arrays/list is probably the worst (look at C). The best way is to treat > > theses caracters uniformaly. > It's not all about performance at all costs though. Agreed. Simply adding a note along the lines of Normally, using this function is a sign that you are doing something extremely inefficient, and you might want to consider another approach. to the ocamldoc should suffice to alert users of possible inefficiency. Frankly, I don't think it's ExtLib's job to prevent users from doing inefficient things, it should simply provide a more comprehensive standard library. (ExtLib *itself* should obviously be as efficient as possible). > For me, most of the time I want to get something done, and speed simply > doesn't matter > - all the programs I'm writing at the moment spend 99.99% of their > time waiting for a remote SOAP server to respond. > > And BTW, it's possible to do the following : > > List.of_enum (String.enum s) > > String.of_enum (List.enum l) Even so, I vote for adding functions for the direct conversions. I mean if it's a FAQ entry it's probably something which is/would be used a lot, right? -- Bardur Arantsson <ba...@im...> <ba...@sc...> - Huh, how'd you get in here? - Well, the door wasn't locked in any *serious* way... Homer Simpson and Security Salesman | The Simpsons |
From: <ju...@su...> - 2005-02-10 19:19:33
|
Rich, | As others have said, these functions are not in the standard library. | However, useful functions like these[1] are available in | Extlib, which you can find here: | | http://sourceforge.net/projects/ocaml-lib/ Thanks. I don't understand why such functions are not part of the standard library. Even if they are very easy to write, they are the kind of functions most anyone _will_ have to write and not having them in the library is inefficient. Talking about efficiency, I've seen the solutions that have been posted, and MHO is that having to recur to procedural constructs for such oviously functional tasks as "implode" and "explode" seems odd. Juanco |
From: Richard J. <ri...@an...> - 2005-02-10 19:41:47
|
On Thu, Feb 10, 2005 at 03:19:13PM -0400, Juancarlo A?ez wrote: > I don't understand why such functions are not part of the standard library. That's exactly the reason why extlib exists - to provide useful functions to the community which are not part of the standard library. Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com |