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 HAA02015; Sat, 23 Jun 2001 07:27:52 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA02011 for ; Sat, 23 Jun 2001 07:27:51 +0200 (MET DST) Received: from hotmail.com (f13.law15.hotmail.com [64.4.23.13]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f5N5Ror26706 for ; Sat, 23 Jun 2001 07:27:50 +0200 (MET DST) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Fri, 22 Jun 2001 22:27:49 -0700 Received: from 65.26.148.207 by lw15fd.law15.hotmail.msn.com with HTTP; Sat, 23 Jun 2001 05:27:48 GMT X-Originating-IP: [65.26.148.207] From: "Jeremy Fincher" To: caml-list@inria.fr Subject: [Caml-list] New to OCaml: can someone explain how this code can be done better? Date: Sat, 23 Jun 2001 01:27:48 -0400 Mime-Version: 1.0 Content-Type: text/plain; format=flowed Message-ID: X-OriginalArrivalTime: 23 Jun 2001 05:27:49.0148 (UTC) FILETIME=[3DCE4DC0:01C0FBA5] Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Since I'm learning OCaml, I've been writing little functions as I get a grasp on each little bit of the syntax. One of these functions is "permute", which takes a string and returns a list of all the permutations of the string. Here's the function: let rec permute_rec f s = if String.length s = 1 then (f ^ s) :: [] else let l = ref [] in for i = 0 to ((String.length s) - 1) do let c = String.sub s i 1 in let rem = (String.sub s 0 i) ^ ((String.sub s (i+1) ((String.length s)-i-1))) in l := (List.append !l (permute_rec (f^c) rem)) done; !l;; let permute = permute_rec "";; Now, I'm sure this isn't the best way to code that function. What I'm hoping is that someone can show me a few things about the function: how to make it tail recursive; a better (less imperative?) method of writing it, and which parts are not consistent with common/standard OCaml idiom. *Anything* I'm shown should at least help me learn, so if you have any comments/opinions, please tell me! Thanks, Jeremy _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr