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=AWL autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id C598ABC0A for ; Thu, 5 Apr 2007 18:41:15 +0200 (CEST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l35GfE6a031272 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Thu, 5 Apr 2007 18:41:15 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1HZV1N-0004Xw-00 for ; Thu, 05 Apr 2007 17:41:13 +0100 Date: Thu, 5 Apr 2007 17:41:13 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] Generators/iterators and lazy evaluation? Message-ID: <20070405164113.GA30660@furbychan.cocan.org> References: <46140EB5.50700@inria.fr> <20070405071652.bcb9be46.mle+ocaml@mega-nerd.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070405071652.bcb9be46.mle+ocaml@mega-nerd.com> User-Agent: Mutt/1.5.9i From: Richard Jones X-Miltered: at concorde with ID 461526AB.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 val:01 mutable:01 iterators:01 wrote:01 caml-list:01 lazy:02 let:03 let:03 erik:04 thu:05 apr:06 red:92 function:08 fun:08 On Thu, Apr 05, 2007 at 07:16:52AM +1000, Erik de Castro Lopo wrote: > 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 Potentially anything which keeps state can be used. I much prefer: let pow2gen = let current = ref 0 in fun () -> current := if !current = 0 then 1 else !current * 2; !current Mainly because it can be inserted anywhere within a function. Rich. -- Richard Jones Red Hat