From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA18968; Tue, 18 Feb 2003 00:16:01 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id AAA18985 for ; Tue, 18 Feb 2003 00:16:00 +0100 (MET) Received: from marble.he.net (marble.he.net [216.218.230.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h1HNFxP04174 for ; Tue, 18 Feb 2003 00:15:59 +0100 (MET) Received: from ucdavis.edu ([208.227.214.58] (may be forged)) by marble.he.net (8.8.6/8.8.2) with ESMTP id PAA21347 for ; Mon, 17 Feb 2003 15:15:53 -0800 Message-ID: <3E516E75.3030703@ucdavis.edu> Date: Mon, 17 Feb 2003 15:21:25 -0800 From: Issac Trotts User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020913 Debian/1.1-1 MIME-Version: 1.0 To: OCaml List Subject: Re: [Caml-list] Re: "ocaml_beginners"::[] string to list References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Brian Hurt wrote: >On Mon, 17 Feb 2003, Issac Trotts wrote: > > > >>Christian Schaller wrote: >> >> >> >>>Hi everyone! >>> >>>I'm trying to find a built in function for converting a string to a list of >>>characters, similar to SML's explode. Is something like this available? >>> >>>Thank you! >>> >>>- Chris >>> >>> >>> >># let explode s = >> let n = String.length s in >> let rec aux k = if k = n then [] else s.[k] :: aux (k+1) in >> aux 0;; >> val explode : string -> char list = >># explode "foo";; >>- : char list = ['f'; 'o'; 'o'] >> >> >> > >let explode s = > let n = String.length s in > let rec aux k accum = if k = n then (List.rev accum) > else aux (k+1) (s.[k] :: accum) > in > aux 0 [] >;; > >See previous discussion. > Good point: we want it to be tail-recursive. Just to clean up, and make it more efficient... let explode s = let rec aux k accum = if k = -1 then accum else aux (k-1) (s.[k] :: accum) in aux (String.length s - 1) [] ;; Issac > >Brian > > > > > ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners