From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id A9E81BC88 for ; Thu, 10 Feb 2005 04:45:40 +0100 (CET) Received: from smtp.syd.swiftdsl.com.au (smtp.syd.swiftdsl.com.au [218.214.224.138]) by nez-perce.inria.fr (8.13.0/8.13.0) with SMTP id j1A3jbXb006527 for ; Thu, 10 Feb 2005 04:45:39 +0100 Received: (qmail 20493 invoked from network); 10 Feb 2005 03:24:14 -0000 Received: from unknown (HELO coltrane.mega-nerd.net) (218.214.64.136) by smtp.syd.swiftdsl.com.au with SMTP; 10 Feb 2005 03:24:14 -0000 Received: from coltrane (localhost [127.0.0.1]) by coltrane.mega-nerd.net (Postfix) with SMTP id 828A57AD8 for ; Thu, 10 Feb 2005 14:24:02 +1100 (EST) Date: Thu, 10 Feb 2005 14:24:01 +1100 From: Erik de Castro Lopo To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] String to list to string Message-Id: <20050210142401.41d60b53.ocaml-erikd@mega-nerd.com> In-Reply-To: References: <420ac293.30127b31.1fa8.0b02@smtp.gmail.com> Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed version 1.0.0 (GTK+ 1.2.10; i386-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 420AD8E1.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 wrote:01 trivial:01 rec:01 pred:01 pred:01 rec:01 stack:01 nospam:98 tail:01 slower:01 strings:01 strings:01 lisp:01 neumann:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.2 X-Spam-Level: On Wed, 9 Feb 2005 19:27:37 -0700 "William D.Neumann" 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)