<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/74f05144c7fc815d582c633fa4b557ac"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-05T03:24:46"
  subject="Re: [Caml-list] lazy computation problem"
  prev="2003/12/ea8e202bbe21bf899ca6dcb2d59b0c8d"
  next="2003/12/8a58951f46d789bf2b5c03ab17428fcf"
  prev-in-thread="2003/12/ea8e202bbe21bf899ca6dcb2d59b0c8d"
  prev-thread="2003/12/e4ffe98ad674659fd303b53d209bccd5"
  next-thread="2003/12/f95e21d4439f6a407a7b0fba2f509364"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] lazy computation problem">
<msg 
  url="2003/12/ea8e202bbe21bf899ca6dcb2d59b0c8d"
  from="Pietro Abate &lt;Pietro.Abate@a...&gt;"
  author="Pietro Abate"
  date="2003-12-05T02:01:07"
  subject="[Caml-list] lazy computation problem">
<msg 
  url="2003/12/74f05144c7fc815d582c633fa4b557ac"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-05T03:24:46"
  subject="Re: [Caml-list] lazy computation problem">
</msg>
</msg>
</thread>

<contents>
&gt; Hi all.
&gt; I'm trying to learn how to programm lazily, but I'm kinda stuck.
&gt; I've a list, say let l = [[1;2;3];[4;5];[6;7;8]] and I want to
&gt; produce all possibile permutations (1,4,6) (1,4,7) (1,4,8) (1,5,6)
&gt; (1,5,7) ...
&gt;
&gt; it can easily be done with List.iter and a couple of recoursive steps,
&gt; but I'm trying to code it in a tail-recoursive style and using lazy
&gt; evaluation. Hence my problem is to write a function that gets the list
&gt; and gives me back one result (1,4,6) and a lazy structure that encode
&gt; the rest of the computation... I looked at lazy streams or lazy lists to
&gt; solve this problem, but I was unable to come up with any nice
&gt; solution...
&gt;
&gt; does anybody have any hints ?
&gt;
&gt; 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
  | [] -&gt; Enum.empty()
  | l :: [] -&gt; Enum.map (fun x -&gt; [x]) (List.enum l)
  | l :: l2 -&gt;
    let e = enum_permut l2 in
    Enum.concat (
        Enum.map (fun x -&gt;
            Enum.map (fun y -&gt; x :: y) (Enum.clone e)
        ) (List.enum l)
    )

let print_list l =
 List.iter (fun i -&gt; 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

</contents>

</message>

