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
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-04 (21:51)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Strings
Alp Mestan <alp@mestan.fr> writes:

> I think providing both capabilities is the best solution.

Phantom types solvethis beautifully with not truntime penalty.


> However, let's study Haskell's strings.
> They simply are a list of characters. This let the ability to use heavily
> list-related functions (take, takeWhile, drop, dropWhile, map, etc.). On the

The beauty of ocaml strings is that they are really compact. An ocaml
string on 32bit is 5-8 bytes longer than the contained string and 9-16
bytes on 64bit.

On the other hand a list of characters is 8/16 times as large. Not to
mention that you have no random access.

> other hand, OCaml's standard library lacks of many functions for strings ! I
> think this is too much imperative oriented. Maybe we could try to implement
> (for Batteries or in a separate project) string lists and then use the power
> of Batteries' list module with many (really many... that's a real pleasure to
> read its documentation) functions to work with. I think with a bit of internal
> laziness, we could get a great immutable string type with many functions to
> manipulate it.
> But I guess there are many cons to do so, otherwise it would have been
> "standardized".

Mutable/Immutable can really nicely done with phantom types and is
independent of the data structure used. It works for strings, lists,
arrays, sets, trees, ... and I think all standard modules should have
it. The official standard lib is rather lacking there but that is why
there is Batteries. The more I hear/see of it the more I like it.

As for char lists for strings you can always convert strings to char
lists when that is a better representation. I've done so myself on a
number of ocasions. But just as often I used strings as char
arrays. Doing e.g. String.blit with char lists would be ugly.