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
Fwd: Re: [Caml-list] The boon of static type checking
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-02-10 (03:40)
From: Jon Harrop <jon@j...>
Subject: Re: [Caml-list] String to list to string
On Thursday 10 February 2005 02:10, Juancarlo Añez wrote:
> Why aren't there functions in the standard library to convert strings to
> lists of characters and back?

Outrageously, the core library fails to provide an arbitrary number of 
arbitrary functions. This exact question came up recently. The answer was 
essentially: Why string -> char list and not string -> char array? Why string 
-> char list and not string -> string list? And so on.

If you want succinct implementations then I'd go for:

let char_list_of_string s =
  let l = ref [] in
  String.iter (fun c -> l := c :: !l) s;
  List.rev !l

let string_of_char_list l =
  String.concat "" (List.map (String.make 1) l)

> Haskell treats strings as lists of chars by default.

I see an optimisation...

Dr Jon D Harrop, Flying Frog Consultancy Ltd.