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 TAA11142; Tue, 22 May 2001 19:03:16 +0200 (MET DST) 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 TAA10995 for ; Tue, 22 May 2001 19:03:15 +0200 (MET DST) Received: from anagyris.wanadoo.fr (smtp-rt-1.wanadoo.fr [193.252.19.151]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f4MH34n18247 for ; Tue, 22 May 2001 19:03:05 +0200 (MET DST) Received: from mahonia.wanadoo.fr (193.252.19.58) by anagyris.wanadoo.fr; 22 May 2001 19:02:58 +0200 Received: from debian (195.6.81.239) by mahonia.wanadoo.fr; 22 May 2001 19:01:24 +0200 Received: from moi by debian with local (Exim 3.22 #1 (Debian)) id 152Gs1-0002pq-00 for ; Tue, 22 May 2001 20:27:01 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] lisp -> ocaml References: <20010522093341.A97425@caddr.com> From: Remi VANICAT Date: 22 May 2001 20:27:01 +0200 In-Reply-To: <20010522093341.A97425@caddr.com> Message-ID: <877kz9kznu.dlv@wanadoo.fr> User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.0.103 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Miles Egan writes: > For my own edification I've been translating some of the code from Norvig's > PAIP AI text to get an idea of what this kind of code looks like in OCaml and > I've come up against two small problems. > > First, I have two mutually recursive function definitions: > > let apply_op op = > if (Stateset.for_all achieve op.op_preconds) then > begin > print_endline ("executing " ^ op.op_action); > global_state := Stateset.diff !global_state op.op_dellist; > global_state := Stateset.union !global_state op.op_addlist; > true > end > else > false > > let achieve goal ops = > Stateset.mem goal global_state or > List.exists apply_op (List.filter (appropriate_p goal) ops) something like let rec apply_op op = if (Stateset.for_all achieve op.op_preconds) then begin print_endline ("executing " ^ op.op_action); global_state := Stateset.diff !global_state op.op_dellist; global_state := Stateset.union !global_state op.op_addlist; true end else false and achieve goal ops = Stateset.mem goal global_state or List.exists apply_op (List.filter (appropriate_p goal) ops) will work > > Which fails to compile, for obvious reasons. Is the solution to refactor the > code into a nonrecursive form? In the absence of forward declarations, I'm > not sure what else to do. the let rec ... = .. and ... = ... and ... = ... make the trick > Lisp provides set operations on lists and allows printing of > symbols, which makes it possible to both manipulate "state" lists > and print them with very little code. I can't use variants with the > same flexibility because I have to provide an explicit ordering if I > want to use them as set members no you haven't, the function compare is a very good ordering for variant: # compare `Aze `Azr ;; - : int = -1 # compare (Some 0) None;; - : int = 1 # type response = Yes | No;; type response = Yes | No # compare Yes No;; - : int = -1 > and I can't print them because there doesn't seem to be a way to > print something like: > > type t = ONE | TWO | THREE > > as "ONE", "TWO", "THREE". yes this a problem. but often there is better way to print thing than just the name of the variant. You can also use camlp4 to make a construct who build the variant and the printer at the same time. -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr