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 UAA01772; Wed, 17 Dec 2003 20:43:06 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA01805 for ; Wed, 17 Dec 2003 20:43:05 +0100 (MET) Received: from mail2.speakeasy.net (mail2.speakeasy.net [216.254.0.202]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hBHJh4X00979 for ; Wed, 17 Dec 2003 20:43:04 +0100 (MET) Received: (qmail 28734 invoked from network); 17 Dec 2003 19:43:02 -0000 Received: from grace.speakeasy.net ([216.254.0.22]) (envelope-sender ) by mail2.speakeasy.net (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 17 Dec 2003 19:43:02 -0000 Date: Wed, 17 Dec 2003 11:42:53 -0800 (PST) From: brogoff@speakeasy.net To: Falk Hueffner cc: "caml-list@inria.fr" Subject: Re: [Caml-list] Python's yield, Lisp's call-cc or C's setjmp/longjmp in OCaml In-Reply-To: <87y8tbvii9.fsf@student.uni-tuebingen.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Loop: caml-list@inria.fr X-Spam: no; 0.00; brogoff:01 caml-list:01 python's:01 lisp's:01 c's:01 falk:01 hueffner:01 ijtrotts:01 incr:01 val:01 val:01 clu:99 printf:01 printf:01 python:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On 17 Dec 2003, Falk Hueffner wrote: > ijtrotts@ucdavis.edu writes: > > > Why not do this: > > > > # let rec number() = > > let i=ref(-1) in fun() -> > > incr i; > > !i, if !i mod 2 = 0 then "Even" else "Odd";; > > val number : unit -> unit -> int * string = > > # let n=number();; > > val n : unit -> int * string = > > # Array.init 10 (fun _ -> n());; > > - : (int * string) array = > > [|(0, "Even"); (1, "Odd"); (2, "Even"); (3, "Odd"); (4, "Even"); (5, "Odd"); > > (6, "Even"); (7, "Odd"); (8, "Even"); (9, "Odd")|] > > This only works for simple examples. Try for example writing a > function which successively yields all possible moves for a chess > board. The "yield" operator really helps there. While I think yield is pretty cool, having used in Sather, which got in from CLU I believe,you can simulate this control flow and more using higher order functions. Sticking with the simple example, but ditching the array and rolling my own streams with core ML yields (ha!) type 'a generator = Nil | Cons of 'a * (unit -> 'a generator);; let rec odd_even i j = if i > j then Nil else Cons(i, (fun () -> odd_even (succ i) j));; let rec print_upto n = function Nil -> () | Cons(i, f) -> if i <= n then let s = if i mod 2 = 0 then "EVEN" else "ODD" in (Printf.printf "(%i, %s)\n" i s; print_upto n (f())) else ();; which has similar semantics to the Python yield, except that the generator is not consumed during the traversal. It should be obvious how to encode that behavior by modifying print_upto to return the remaining generator. In any case, programming this kind of thing in ML is pretty easy, so I don't think we need an extension to the language for yield. callcc is more powerful, but I don't like the possibility that making callcc fast penalizes code which doesn't use it. We have exceptions and threads already. -- Brian ------------------- 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