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 VAA02683; Wed, 17 Dec 2003 21:04:11 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA02759 for ; Wed, 17 Dec 2003 21:04:09 +0100 (MET) Received: from mail.davidb.org (adsl-64-172-240-129.dsl.sndg02.pacbell.net [64.172.240.129]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hBHK48H28066; Wed, 17 Dec 2003 21:04:08 +0100 (MET) Received: from davidb by mail.davidb.org with local (Exim 3.36 #1 (Debian)) id 1AWhtu-0001BS-00; Wed, 17 Dec 2003 12:04:06 -0800 Date: Wed, 17 Dec 2003 12:04:06 -0800 From: David Brown To: Pierre Weis Cc: Falk Hueffner , caml-list@inria.fr Subject: Re: [Caml-list] Python's yield, Lisp's call-cc or C's setjmp/longjmp in OCaml Message-ID: <20031217200406.GA4424@davidb.org> References: <87y8tbvii9.fsf@student.uni-tuebingen.de> <200312171914.UAA00389@pauillac.inria.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200312171914.UAA00389@pauillac.inria.fr> User-Agent: Mutt/1.5.4i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 caml-list:01 python's:01 lisp's:01 c's:01 pierre:01 weis:01 -rectypes:01 printf:01 printf:01 ocamlp:01 iterative:01 recursively:01 ocaml:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wed, Dec 17, 2003 at 08:14:21PM +0100, Pierre Weis wrote: > [...] > > 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. > > Very interesting: please give us the code corresponding to this > example, in order for us to realize how the full power of the "yield" > operator helps to solve this problem. Perhaps this isn't the best example, since the problem is so large. The icon language has been around with a similar operator for quite a while. It is very useful for prolog-type backtracking, since the code can be written fairly naturally. However, this construct is fairly easy to implement in regular OCaml. The following works, but requires -rectypes to compile: open Printf type 'a thump = 'a * (unit -> 'a thump) (* Return the numbers a through b and stop. *) let through a b () : int thump = let rec loop num () = if num > b then raise End_of_file; num, loop (num + 1) in loop a () (* Sample to show the results. *) let show () = let rec loop op = let num, op = op () in printf "%d," num; loop op in try loop (through 1 10) with End_of_file -> printf "\n" let () = show () With a bit more work, you can write it without needing the rectypes. If the construct is needed, perhaps it could be done in ocamlp4. It would probably have to rewrite the iterative constructs recursively, though. Dave ------------------- 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