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 HAA29386; Wed, 17 Dec 2003 07:29:27 +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 HAA29480 for ; Wed, 17 Dec 2003 07:29:25 +0100 (MET) Received: from pop19.ucdavis.edu (pop19.ucdavis.edu [169.237.105.29]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hBH6TNX05881 for ; Wed, 17 Dec 2003 07:29:24 +0100 (MET) Received: from 128.120.141.214 ([128.120.141.214]) by pop19.ucdavis.edu (8.12.10/8.12.9/it-std-5.2.0) with ESMTP id hBH6TLbV026247 for ; Tue, 16 Dec 2003 22:29:21 -0800 (PST) From: ijtrotts@ucdavis.edu To: caml-list@inria.fr Subject: Re: [Caml-list] Python's yield, Lisp's call-cc or C's setjmp/longjmp in OCaml Date: Tue, 16 Dec 2003 22:30:23 -0800 User-Agent: KMail/1.5.4 References: <203E10E3-2FF3-11D8-A94A-000393CFE6B8@spy.net> In-Reply-To: <203E10E3-2FF3-11D8-A94A-000393CFE6B8@spy.net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200312162230.23322.ijtrotts@ucdavis.edu> X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ijtrotts:01 caml-list:01 python's:01 lisp's:01 c's:01 ocaml's:01 c's:01 python's:01 incr:01 val:01 val:01 issac:01 ocaml:01 ocaml:01 int:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tuesday 16 December 2003 10:10, Dustin Sallings wrote: > On Dec 16, 2003, at 10:42, Brian Hurt wrote: > > By my measurements, Ocaml's exceptions are faster than C's > > setjmp/longjmp. > > Ocaml doesn't provide first-class continuations, but most of the things > > people actually do with call-cc can be done in other ways in Ocaml. I > > don't know what Python's yield instruction does. > > Simple example: > > from __future__ import generators > > def number(max): > for i in range(max): > if(i % 2 == 0): > yield i, "Even" > else: > yield i, "Odd" > > for n in number(10): > print n > > The output of this example is as follows: > > (0, 'Even') > (1, 'Odd') > (2, 'Even') > (3, 'Odd') > (4, 'Even') > (5, 'Odd') > (6, 'Even') > (7, 'Odd') > (8, 'Even') > (9, 'Odd') > > It's basically a special case of an upward continuation (or is it > downward? I'm bad with terminology). I think you can get pretty close > to this behavior with streams, but it's still not as easy as a return > statement that has a resume type thing. 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")|] ? Issac ------------------- 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