<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/f43d6ac0112ef047d18d7386d09e7f89"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-12-17T19:43:06"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml"
  prev="2003/12/8edface9abeaaa61e00ef1a62a6c512c"
  next="2003/12/777e01692302f33698e7b6cb5d3e6839"
  prev-in-thread="2003/12/93c3b2ec873addb64754bf56a09fb0a7"
  next-in-thread="2003/12/e9e19245af4d01fc23cbaecd9a62ba86"
  prev-thread="2003/12/774646ec595322579ea76517edc01410"
  next-thread="2003/12/49cb44539cd1e5dc776c6a8f345e0a04"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/f0d20744dca590b72fee31849f8ef5a8"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-16T13:13:20"
  subject="[Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/bf33bfec2c564f71b03f23afcf441aca"
  from="Oleg Trott &lt;oleg_trott@c...&gt;"
  author="Oleg Trott"
  date="2003-12-16T13:28:13"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/052649193f7443e5fad49ddd9dbe05f7"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-18T00:15:15"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
</msg>
<msg 
  url="2003/12/e8f2fdd8daaa77e7297afe895e600af3"
  from="Ville-Pertti Keinonen &lt;will@e...&gt;"
  author="Ville-Pertti Keinonen"
  date="2003-12-16T13:48:01"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/01e78a00f853a1ce4d39b398b9afb114"
  from="Kenneth Knowles &lt;kknowles@u...&gt;"
  author="Kenneth Knowles"
  date="2003-12-16T15:44:06"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/e9d5dc1f43b97ae6ac0b88e55734a825"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2003-12-16T16:45:41"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/346cf0f17cf25f0a13da2cfa8edf4234"
  from="Ville-Pertti Keinonen &lt;will@e...&gt;"
  author="Ville-Pertti Keinonen"
  date="2003-12-16T18:36:42"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2003/12/88fc2c8210caaa7c1abe39d783755b7f"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-16T17:40:58"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/40790bf3984b821df7900e88d90f42ac"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-12-16T18:10:39"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/5d2b8084aba6efbe7ecad855a7b3fddc"
  from="ijtrotts@u..."
  author="ijtrotts@u..."
  date="2003-12-17T06:29:27"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/1f4371b3bf98c5187ba2a9e7dbda1b49"
  from="Dustin Sallings &lt;dustin@s...&gt;"
  author="Dustin Sallings"
  date="2003-12-17T08:13:15"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
<msg 
  url="2003/12/850360ffe8d0d409f2af993875710c0d"
  from="Falk Hueffner &lt;falk.hueffner@s...&gt;"
  author="Falk Hueffner"
  date="2003-12-17T10:36:03"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/0d6d04a011e30722187b8606b72e19f6"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2003-12-17T19:14:30"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/8edface9abeaaa61e00ef1a62a6c512c"
  from="Falk Hueffner &lt;falk.hueffner@s...&gt;"
  author="Falk Hueffner"
  date="2003-12-17T19:32:23"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
<msg 
  url="2003/12/777e01692302f33698e7b6cb5d3e6839"
  from="David Brown &lt;caml-list@d...&gt;"
  author="David Brown"
  date="2003-12-17T20:04:11"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
<msg 
  url="2003/12/ac2f915dbdd9a3f399cc73b8be16246d"
  from="Nicolas Cannasse &lt;warplayer@f...&gt;"
  author="Nicolas Cannasse"
  date="2003-12-18T01:15:27"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/744adfd5672c3969872e3d4872e7741f"
  from="David Brown &lt;caml-list@d...&gt;"
  author="David Brown"
  date="2003-12-18T05:32:02"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
<msg 
  url="2003/12/1c4b3799107085f07200a724dd0cc444"
  from="Brian Hurt &lt;bhurt@s...&gt;"
  author="Brian Hurt"
  date="2003-12-18T06:27:21"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/4a1681fc89e77e8a070727585335ceeb"
  from="David Brown &lt;caml-list@d...&gt;"
  author="David Brown"
  date="2003-12-18T06:46:06"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
</msg>
<msg 
  url="2003/12/93c3b2ec873addb64754bf56a09fb0a7"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-12-18T18:44:30"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
</msg>
</msg>
<msg 
  url="2003/12/f43d6ac0112ef047d18d7386d09e7f89"
  from="brogoff@s..."
  author="brogoff@s..."
  date="2003-12-17T19:43:06"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
<msg 
  url="2003/12/e9e19245af4d01fc23cbaecd9a62ba86"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-12-19T13:40:06"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
</msg>
</msg>
<msg 
  url="2003/12/a8c52bf4ae153acf2af4c9fccd7a479c"
  from="Nuutti Kotivuori &lt;naked+caml@n...&gt;"
  author="Nuutti Kotivuori"
  date="2003-12-18T00:51:23"
  subject="Re: [Caml-list] Python&apos;s yield, Lisp&apos;s call-cc or C&apos;s setjmp/longjmp in OCaml">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
On 17 Dec 2003, Falk Hueffner wrote:
&gt; ijtrotts@ucdavis.edu writes:
&gt;
&gt; &gt; Why not do this:
&gt; &gt;
&gt; &gt; # let rec number() =
&gt; &gt;    let i=ref(-1) in fun() -&gt;
&gt; &gt;     incr i;
&gt; &gt;     !i, if !i mod 2 = 0 then "Even" else "Odd";;
&gt; &gt; val number : unit -&gt; unit -&gt; int * string = &lt;fun&gt;
&gt; &gt; # let n=number();;
&gt; &gt; val n : unit -&gt; int * string = &lt;fun&gt;
&gt; &gt; # Array.init 10 (fun _ -&gt; n());;
&gt; &gt; - : (int * string) array =
&gt; &gt; [|(0, "Even"); (1, "Odd"); (2, "Even"); (3, "Odd"); (4, "Even"); (5, "Odd");
&gt; &gt;   (6, "Even"); (7, "Odd"); (8, "Even"); (9, "Odd")|]
&gt;
&gt; This only works for simple examples. Try for example writing a
&gt; function which successively yields all possible moves for a chess
&gt; 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 -&gt; 'a generator);;

let rec odd_even i j =
  if i &gt; j then
    Nil
  else
    Cons(i, (fun () -&gt; odd_even (succ i) j));;

let rec print_upto n = function
    Nil -&gt; ()
  | Cons(i, f) -&gt;
      if i &lt;= 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

</contents>

</message>

