Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] lisp -> ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi VANICAT <vanicat@l...>
Subject: Re: [Caml-list] lisp -> ocaml
Miles Egan <miles@caddr.com> 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