Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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.