English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Strings as arrays or lists...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-02-28 (01:03)
From: brogoff@s...
Subject: Re: [Caml-list] Strings as arrays or lists...
On Thu, 27 Feb 2003, Oliver Bandel wrote:
> Hello,
> in Haskell, strings are lists of chars.

And what a horrible design decision that is! Even the other 
lazy language (Clean) doesn't screw this one up, and last I looked 
at it had strings as unboxed character arrays. 

> In Ocaml, strings seem to be array-like,
> but it's not possible to apply Array-functions
> on Strings, but nevertheless, the char's of a
> string are used in a very similar way, as arrays.

Think of them as packed character arrays. 

> The indexing is done with .(idx) in the one case
> and with .[idx] in the other case.

OK, so some of us will be much happier if (when?) OCaml implements 
extensional polymorphism and we can treat all array like thingies uniformly. 

> Well, when strings would be lists of chars,
> List-commands like List.map and List.filter
> could be used.

Oh really? And what do we do when we want to use List.map on a long string? 
On my machine, Sys.max_string_length is 16_777_211. List.map will crap out 
on a much smaller list. 

> This would be more FPL-like. (And more convenient
> and IMHO more consistently.)

It would also be way slower, and restrict strings to much smaller lengths.
The length restriction can (and should!) be fixed by extending the set of 
functions which are tail recursive, as has been discussed on this list in 
the last month. However, I think array like representations for strings make 
more sense, and if "FPL" means "handles arrays poorly" then I think the flaw 
is in FPLs. 

> But even if only the imperative Array-access would
> be possible (instead of the FPL-like Lists), it would
> be more convenient then to have such a string-type,
> which can't be used with the many libraries.

I can't quite parse this, but if you mean that you'd like to treat strings 
like arrays notationally, I agree. Agitate for extensional polymorphism. 
Wow, that sounds even better than "eschew obfuscation"!

> Any suggestions on that topic?

There is no one string representation which will satisfy everyone. I prefer 
the built in one to be array based, like in OCaml. You can easily write a 
function like this 

let explode s =
  let rec exap n l =
    if n < 0 then l else
    exap (n - 1) ((String.get s n)::l) in
  exap (String.length s - 1) []

to get the char list you want. You couldn't get the packed char array from 
a char list if it wasn't built in. 

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