From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 193E6BC6B for ; Wed, 4 Apr 2007 23:17:06 +0200 (CEST) Received: from smtp.syd.people.net.au (smtp.syd.people.net.au [218.214.225.98]) by discorde.inria.fr (8.13.6/8.13.6) with SMTP id l34LH2ua031265 for ; Wed, 4 Apr 2007 23:17:04 +0200 Received: (qmail 19993 invoked from network); 4 Apr 2007 21:16:54 -0000 Received: from unknown (HELO coltrane.mega-nerd.net) (218.214.64.136) by smtp.syd.people.net.au with SMTP; 4 Apr 2007 21:16:54 -0000 Received: from coltrane (coltrane [192.168.1.101]) by coltrane.mega-nerd.net (Postfix) with SMTP id 213827B68 for ; Thu, 5 Apr 2007 07:16:53 +1000 (EST) Date: Thu, 5 Apr 2007 07:16:52 +1000 From: Erik de Castro Lopo To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Generators/iterators and lazy evaluation? Message-Id: <20070405071652.bcb9be46.mle+ocaml@mega-nerd.com> In-Reply-To: <46140EB5.50700@inria.fr> References: <46140EB5.50700@inria.fr> Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed version 2.3.0beta5 (GTK+ 2.8.20; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 461415CE.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 frisch:01 iterator:01 runtime:01 threads:01 computations:01 stack:01 python's:01 python's:01 ocaml:01 def:01 val:01 mutable:01 printf:01 printf:01 Alain Frisch wrote: > I think you cannot implement this notion of iterator without some kind > of control flow operator (at runtime) or some code rearrangement (at > compile time). The point is that you must mainting several active > "threads" of computations, including their own stack. Alain, I think you may have misunderstood Python's yeild operator. Typically the original poster's pow2 generators would be used like: for p = pow2 (10): print p but I really don't see how laziness Python's generators actually provide any benefit for this problem over the more obvious Python solution to this which is: for i = range (10): p = 2 ^ i print p The problem with the above is that generators (and laziness in Ocaml) really only make sense for sequences which are effecitvely infinite. In the Python case, that would be: def pow2 (): start = 0 yield 2^start start += 1 One ocaml solution to this is an class/object: class pow2gen = object val mutable current = 0 method next () = current <- if current == 0 then 1 else current * 2 ; current end which can be used like this: let _ = let pow2 = new pow2gen in for k = 0 to 10 do Printf.printf "%d : %d\n" k (pow2#next ()) ; done There would also be a lazy solution to this problem but I don't think it would be a better solution that the object soultion above. I've got a more realistic example of lazy lists on my blog: http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/lazy_lists.html Hope this helps, Erik -- +-----------------------------------------------------------+ Erik de Castro Lopo +-----------------------------------------------------------+ "Every time microshaft's stock price drops again, I rejoice. I want to see that bunch of criminals brought to their knees. Preferably at the chopping block." -- rixt in http://linuxtoday.com/stories/20659_flat.html