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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: leary@n...
Subject: Re: [Caml-list] Newbie: declarations
On Sun, Jun 17, 2001 at 02:08:28PM +0400, Vitaly Lugovsky wrote:
>  It's an almost functional language, so please avoid variables. And use
> references only when you really need 'em. Refer to the Guide about
> references and named tuples.

Here's what I'm doing now (comments welcome; yes, I have short cryptic
names).  I'm writing a robot for RealTimeBattle, and want to have access to
information that isn't contained in every message the robot gets (and I'm
not using threads or signals yet, fwiw).  This information is updated all
the time.  (I'll post a link to the whole thing for criticism, once it
actually does something interesting. :)

type datarec = {
	mutable name : string;
	mutable color : string;
	mutable bt_loc : float * float * float;
	mutable bt_dir : float;
	mutable bt_rot : int; (* 1 clockwise -1 counterclockwise *)
	mutable cn_rot : int; (* 1 clockwise -1 counterclockwise *)
	mutable rd_rot : float; (* 1 clockwise -1 counterclockwise *)
	mutable rd_iff : int; (* 1 friend 0 foe *)
	mutable rd_swp : float * float;
	mutable rd_cnt : int;
	mutable rd_ang : float;
	mutable speed : float;
	mutable ca : float;
	mutable time : float;
	mutable en_loc : coord;
	mutable en_dir : float;
	mutable en_nrg : float;
	mutable mn_loc : coord;
	mutable mn_dir : float;
	mutable ck_loc : coord;
	mutable ck_dir : float;
};;

let data : datarec = {
	name = "bob";
	color = "00FF00";
	bt_loc = (0.0, 0.0, 0.0);
	bt_dir = 0.0;
	bt_rot = 0;
	cn_rot = 0;
	rd_rot = 1.0;
	rd_iff = 0;
	rd_swp = (90.0, 90.0);
	rd_cnt = 0;
	rd_ang = 90.0;
	speed = 0.0;
	ca = 0.0;
	time = 0.0;
	en_loc = (0.0, 0.0);
	en_dir = 0.0;
	en_nrg = 0.0;
	mn_loc = (0.0, 0.0);
	mn_dir = 0.0;
	ck_loc = (0.0, 0.0);
	ck_dir = 0.0;
	};;
 

(* Better to have 'let initialize = function....'  for f() on a single
variable? *)

let initialize i = match i with
	1 -> name "Bob"; color "00FF00 FF0000";
		debug "initialize 1\n";
	| 0 -> 
		data.name <- "bob";
		data.color <- "00FF00";
		data.bt_loc <- (0.0, 0.0, 0.0);
		data.bt_dir <- 0.0;
		data.bt_rot <- 0;
		data.cn_rot <- 0;
		data.rd_rot <- 1.0;
		data.rd_iff <- 0;
		data.rd_swp <- (90.0, 90.0);
		data.rd_cnt <- 0;
		data.rd_ang <- 90.0;
		data.speed <- 0.0;
		data.ca <- 0.0;
		data.time <- 0.0;
		data.en_loc <- (0.0, 0.0);
		data.en_dir <- 0.0;
		data.en_nrg <- 0.0;
		data.mn_loc <- (0.0, 0.0);
		data.mn_dir <- 0.0;
		data.ck_loc <- (0.0, 0.0);
		data.ck_dir <- 0.0 ;
		debug "initialize 0\n";
	| _ -> ();;



-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr