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: Erik de Castro Lopo <ocaml-erikd@m...>
Subject: Re: [Caml-list] String to list to string
On Wed, 9 Feb 2005 19:27:37 -0700
"William D.Neumann" <wneumann@cs.unm.edu> wrote:

> Because a) they're not all that useful and b) they're trivial to write 
> for yourself:

Agree on both counts.

> let explode s =
>    let rec exh acc = function
>    | -1 -> acc
>    | i -> exh (s.[i]::acc) (pred i)
>    in exh [] (pred (String.length s))

Here's one thats a little more obvious (remove the function, use String.get)
and runs about 20% faster (at least on my iBook running Linux):

let string_to_charlist str =
	let rec stc lst n =
		if n < 0 then lst
		else stc ((String.get str n) :: lst) (n - 1)
	in
	stc [] ((String.length str) - 1)
	;;

To be honest, this was my second attempt. My first attempt was slower
than yours and blew the stack on million character strings (obviously
not tail rescursive). This one (and yours) is quite happy with strings 
10 times that size.

Erik
-- 
+-----------------------------------------------------------+
  Erik de Castro Lopo  nospam@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
C++ is a siren song. It *looks* like a HLL in which you ought to be
able to write an application, but it really isn't."
-- Alain Picard (comp.lang.lisp)