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 EAA24874; Fri, 5 Dec 2003 04:24:46 +0100 (MET) 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 EAA24093 for ; Fri, 5 Dec 2003 04:24:44 +0100 (MET) Received: from smtp.siren.ocn.ne.jp (siren.ocn.ne.jp [211.129.13.170]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hB53Ogr10010 for ; Fri, 5 Dec 2003 04:24:43 +0100 (MET) Received: from PWARP (p1128-ipad08kyoto.kyoto.ocn.ne.jp [221.189.198.128]) by smtp.siren.ocn.ne.jp (Postfix) with SMTP id 5E69874D; Fri, 5 Dec 2003 12:24:39 +0900 (JST) Message-ID: <006001c3badf$4fdca740$0274a8c0@PWARP> From: "Nicolas Cannasse" To: "Pietro Abate" , "ocaml ml" References: <20031205020112.GA11913@anu.edu.au> Subject: Re: [Caml-list] lazy computation problem Date: Fri, 5 Dec 2003 12:24:37 +0900 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Loop: caml-list@inria.fr X-Spam: no; 0.00; cannasse:01 warplayer:01 caml-list:01 lazily:01 possibile:99 permutations:01 extlib:01 sourceforge:01 enum:01 enum:01 clone:01 encode:01 nicolas:01 int:01 rec:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > Hi all. > I'm trying to learn how to programm lazily, but I'm kinda stuck. > I've a list, say let l = [[1;2;3];[4;5];[6;7;8]] and I want to > produce all possibile permutations (1,4,6) (1,4,7) (1,4,8) (1,5,6) > (1,5,7) ... > > it can easily be done with List.iter and a couple of recoursive steps, > but I'm trying to code it in a tail-recoursive style and using lazy > evaluation. Hence my problem is to write a function that gets the list > and gives me back one result (1,4,6) and a lazy structure that encode > the rest of the computation... I looked at lazy streams or lazy lists to > solve this problem, but I was unable to come up with any nice > solution... > > does anybody have any hints ? > > p Here's a nice solution, using Enum's from the Extlib ( http://ocaml-lib.sourceforge.net ). Purely lazy :-) open ExtList let rec enum_permut = function | [] -> Enum.empty() | l :: [] -> Enum.map (fun x -> [x]) (List.enum l) | l :: l2 -> let e = enum_permut l2 in Enum.concat ( Enum.map (fun x -> Enum.map (fun y -> x :: y) (Enum.clone e) ) (List.enum l) ) let print_list l = List.iter (fun i -> print_int i; print_string ",") l; print_newline() ;; let l = [[1;2;3];[4;5];[6;7;8]] in let e = enum_permut l in Enum.iter print_list e ------------------- 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