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 discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 731B4BC0A for ; Thu, 5 Apr 2007 01:58:37 +0200 (CEST) Received: from pih-relay04.plus.net (pih-relay04.plus.net [212.159.14.131]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l34Nwaeq029126 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Thu, 5 Apr 2007 01:58:37 +0200 Received: from [80.229.56.224] (helo=beast.local) by pih-relay04.plus.net with esmtp (Exim) id 1HZFN6-00081a-DA for caml-list@yquem.inria.fr; Thu, 05 Apr 2007 00:58:36 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Generators/iterators and lazy evaluation? Date: Thu, 5 Apr 2007 00:53:41 +0100 User-Agent: KMail/1.9.5 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200704050053.41426.jon@ffconsultancy.com> X-Miltered: at discorde with ID 46143BAC.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; arbitrarily:01 def:01 ocaml:01 ocaml:01 -rectypes:01 camlp:01 camlp:01 parsing:01 val:01 val:01 -rectypes:01 parsing:01 syntax:01 frog:98 iterators:01 On Wednesday 04 April 2007 17:33, Raj B wrote: > A generator in Python can be thought of as an arbitrarily generated > 'lazy list'. As an example > the following is a generator capable of generating powers of 2 upto a > max value. > > def pow2(max): > start = 0 > while (start .lt. max): > yield 2^start > start += 1 > > The 'yield' statement is the point where the function returns the > next value and suspends itself until the next time it is 'forced'. At > that time it resumes execution where it left off. > > OCaml makes this particularly hard to implement this due to lack of > 'control flow' features Might I suggest deferring judgement until you have seen some OCaml solutions. $ ocaml -rectypes camlp4o.cma Objective Caml version 3.10.0+beta Camlp4 Parsing version 3.10.0+beta # Use a lazy list sum type: # type 'a llist = Nil | Cons of 'a * 'a llist lazy_t;; type 'a llist = Nil | Cons of 'a * 'a llist lazy_t # let rec pow2 ?(i=1) n = if n>0 then Cons(i, lazy(pow2 ~i:(2*i) (n-1))) else Nil;; val pow2 : ?i:int -> int -> int llist = For example: # let rec list_of = function | Nil -> [] | Cons(h, t) -> h :: list_of(Lazy.force t);; val list_of : 'a llist -> 'a list = # list_of(pow2 10);; - : int list = [1; 2; 4; 8; 16; 32; 64; 128; 256; 512] Use an inferred lazy list type (polymorphic variant): # let rec pow2 ?(i=1) n = if n>0 then `Cons(i, lazy(pow2 ~i:(2*i) (n-1))) else `Nil;; val pow2 : ?i:int -> int -> ([> `Cons of int * 'a lazy_t | `Nil ] as 'a) = Return a lazy tail (using -rectypes): # let rec pow2 ?(i=1) n = if n>0 then Some(i, lazy(pow2 ~i:(2*i) (n-1))) else None;; val pow2 : ?i:int -> int -> ((int * 'a lazy_t) option as 'a) = Return a functional tail (using -rectypes): # let rec pow2 ?(i=1) j n () = if n>0 then Some(i, pow2 ~i:(2*i) (n-1)) else None;; val pow2 : ?i:int -> int -> (int -> unit -> (int * 'a) option as 'a) = Return a lazy stream (using camlp4 stream parsing syntax extension): # let rec pow2 ?(i=1) n = if i] else [<>];; val pow2 : ?i:int -> int -> int Stream.t = The moral is: don't try to write idiomatic Python in OCaml. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. OCaml for Scientists http://www.ffconsultancy.com/products/ocaml_for_scientists