* [Caml-list] Newbie: declarations @ 2001-06-17 0:09 leary 2001-06-17 10:08 ` Vitaly Lugovsky ` (2 more replies) 0 siblings, 3 replies; 12+ messages in thread From: leary @ 2001-06-17 0:09 UTC (permalink / raw) To: caml Is there a way to declare a variable and not initialize it, like C? int i; i = 0; More to the point, is there a way to declare a record variable without listing/initializing all the fields? If yes, what are the default values? ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-17 0:09 [Caml-list] Newbie: declarations leary @ 2001-06-17 10:08 ` Vitaly Lugovsky 2001-06-18 2:19 ` leary 2001-06-17 15:33 ` [Caml-list] Newbie: declarations David Fox 2001-06-19 8:11 ` Frédéric van der Plancke 2 siblings, 1 reply; 12+ messages in thread From: Vitaly Lugovsky @ 2001-06-17 10:08 UTC (permalink / raw) To: leary; +Cc: caml On Sat, 16 Jun 2001 leary@nwlink.com wrote: > Is there a way to declare a variable and not initialize it, like C? > > int i; > i = 0; 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. > More to the point, is there a way to declare a record variable without > listing/initializing all the fields? If yes, what are the default values? You have to initialize 'em always. 'cause you're using a strictly typed language. ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-17 10:08 ` Vitaly Lugovsky @ 2001-06-18 2:19 ` leary 2001-06-18 15:18 ` Remi VANICAT 0 siblings, 1 reply; 12+ messages in thread From: leary @ 2001-06-18 2:19 UTC (permalink / raw) To: Vitaly Lugovsky; +Cc: caml 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-18 2:19 ` leary @ 2001-06-18 15:18 ` Remi VANICAT 2001-06-18 20:19 ` [Caml-list] Good introduction for the working programmer in Objective CAML Mattias Waldau 0 siblings, 1 reply; 12+ messages in thread From: Remi VANICAT @ 2001-06-18 15:18 UTC (permalink / raw) To: caml leary@nwlink.com writes: > 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. :) [...] > let data : datarec = { the type annotation is not mandatory : let data = is enough [...] > > let initialize i = match i with > 1 -> name "Bob"; color "00FF00 FF0000"; > debug "initialize 1\n"; > | 0 -> > data.name <- "bob"; [...] it may be better to have two function : one to initialize the robot, the other to make the other stuff. -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* [Caml-list] Good introduction for the working programmer in Objective CAML 2001-06-18 15:18 ` Remi VANICAT @ 2001-06-18 20:19 ` Mattias Waldau 2001-06-18 20:35 ` Miles Egan 2001-06-18 20:49 ` leary 0 siblings, 2 replies; 12+ messages in thread From: Mattias Waldau @ 2001-06-18 20:19 UTC (permalink / raw) To: caml Someone on this list pointed to www.cs.caltech.edu/cs134/cs134b/book.pdf I read it and it is actually the best english introduction to Ocaml I seen sofar. Short, concise and doesn't try to explain functional programming so much, it just focuses on getting you up and running. I think there should be a link on the first page of www.ocaml.org recommending it. /mattias waldau ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Good introduction for the working programmer in Objective CAML 2001-06-18 20:19 ` [Caml-list] Good introduction for the working programmer in Objective CAML Mattias Waldau @ 2001-06-18 20:35 ` Miles Egan 2001-06-18 20:49 ` leary 1 sibling, 0 replies; 12+ messages in thread From: Miles Egan @ 2001-06-18 20:35 UTC (permalink / raw) To: Mattias Waldau; +Cc: caml On Mon, Jun 18, 2001 at 10:19:36PM +0200, Mattias Waldau wrote: > Someone on this list pointed to > > www.cs.caltech.edu/cs134/cs134b/book.pdf > > I read it and it is actually the best english introduction to > Ocaml I seen sofar. This is an excellent introduction. I had no idea it existed. I'm mailing the link to all the programmers in our department right now... -- miles ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Good introduction for the working programmer in Objective CAML 2001-06-18 20:19 ` [Caml-list] Good introduction for the working programmer in Objective CAML Mattias Waldau 2001-06-18 20:35 ` Miles Egan @ 2001-06-18 20:49 ` leary 1 sibling, 0 replies; 12+ messages in thread From: leary @ 2001-06-18 20:49 UTC (permalink / raw) To: caml On Mon, Jun 18, 2001 at 10:19:36PM +0200, Mattias Waldau wrote: > www.cs.caltech.edu/cs134/cs134b/book.pdf > I think there should be a link on the first page of www.ocaml.org > recommending it. Yes! And may I suggest that the ocaml.org web page be reorganized a bit? It's always seemed a bit confusing and disorganized to me. Should I write up a new version for comparison? Suggestions? note: I have zero power to change the actual page :) ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-17 0:09 [Caml-list] Newbie: declarations leary 2001-06-17 10:08 ` Vitaly Lugovsky @ 2001-06-17 15:33 ` David Fox 2001-06-18 14:52 ` FabienFleutot 2001-06-19 8:11 ` Frédéric van der Plancke 2 siblings, 1 reply; 12+ messages in thread From: David Fox @ 2001-06-17 15:33 UTC (permalink / raw) To: leary; +Cc: caml leary@nwlink.com writes: > Is there a way to declare a variable and not initialize it, like C? > > int i; > i = 0; > > More to the point, is there a way to declare a record variable without > listing/initializing all the fields? If yes, what are the default values? This is a problem I have run into when translating programs from non functional languages. My first pass solution is to create a reference to a dummy object: let dummy = {field1=0; field2=0...} let current = ref dummy ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-17 15:33 ` [Caml-list] Newbie: declarations David Fox @ 2001-06-18 14:52 ` FabienFleutot 0 siblings, 0 replies; 12+ messages in thread From: FabienFleutot @ 2001-06-18 14:52 UTC (permalink / raw) To: leary, David Fox; +Cc: caml From: "David Fox" <dsf@foxthompson.net> > leary@nwlink.com writes: > > > Is there a way to declare a variable and not initialize it, like C? > > > > int i; > > i = 0; > > > > More to the point, is there a way to declare a record variable without > > listing/initializing all the fields? If yes, what are the default values? > [...] > My first pass solution is to create a reference > to a dummy object: > > let dummy = {field1=0; field2=0...} > let current = ref dummy A better solution would be to use options. If it is possible for some of your vars to be uninitialized, this unitialized state should be able to be easily filtered: let current = ref None; ... current := Some(generate_struct foo bar); ... match current with | None -> failwith "This shouldn't be uninitializable at this stage." | Some current -> ... It's way cleaner than if(!current==dummy) tests. ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-17 0:09 [Caml-list] Newbie: declarations leary 2001-06-17 10:08 ` Vitaly Lugovsky 2001-06-17 15:33 ` [Caml-list] Newbie: declarations David Fox @ 2001-06-19 8:11 ` Frédéric van der Plancke 2001-06-19 10:22 ` Sven LUTHER 2 siblings, 1 reply; 12+ messages in thread From: Frédéric van der Plancke @ 2001-06-19 8:11 UTC (permalink / raw) Cc: caml [Excuse-me if this is a duplicate. I haven't seen the previous instance of this message on the list yet, I think I sent it to the wrong address. This message contains additional thoughts anyway ;-)] leary@nwlink.com wrote: > More to the point, is there a way to declare a record variable without > listing/initializing all the fields? If yes, what are the default values? You can create a default record and initialize new records from a copy of the default record like this: type datarec = { mutable name : string; mutable color : string; mutable value : int; } let default_datarec = { name = "?"; color = "?"; value = 0; } let x = { default_datarec with value = 113 } let y = { default_datarec with name = "y"; color = "00FF00 FF00FF" } Beware of aliasing though: the name string is mutable and is shared by all instances for which no specific name was given, so: # x.name.[0] <- '!';; - : unit = () # default_datarec.name;; - : string = "!" Furthermore "someone" may directly modify the default_datarec in your back. This is not the safest way to do it. Unless you do this: let default_datarec () = { name = String.copy "?"; (* Need to copy in order to avoid aliasing *) color = String.copy "?"; value = 0; } let x = let d = default_datarec () in { d with value = 113 } One can't write { default_datarec () with value = 113 } ... And BTW, I like Python's immutable strings ! (Wouldn't be long to implement an immutable-strings module in OCaml though.) /Frederic vdP ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-19 8:11 ` Frédéric van der Plancke @ 2001-06-19 10:22 ` Sven LUTHER 2001-06-19 12:25 ` Frank Atanassow 0 siblings, 1 reply; 12+ messages in thread From: Sven LUTHER @ 2001-06-19 10:22 UTC (permalink / raw) To: Frédéric van der Plancke; +Cc: caml On Tue, Jun 19, 2001 at 10:11:11AM +0200, Frédéric van der Plancke wrote: > [Excuse-me if this is a duplicate. I haven't seen the previous instance > of this message on the list yet, I think I sent it to the wrong address. > This message contains additional thoughts anyway ;-)] > > leary@nwlink.com wrote: > > More to the point, is there a way to declare a record variable without > > listing/initializing all the fields? If yes, what are the default values? > > You can create a default record and initialize new records from a copy > of the default record like this: > > type datarec = { > mutable name : string; > mutable color : string; > mutable value : int; > } > > let default_datarec = { name = "?"; color = "?"; value = 0; } > > let x = { default_datarec with value = 113 } > let y = { default_datarec with name = "y"; color = "00FF00 FF00FF" } > > Beware of aliasing though: the name string is mutable and is shared by > all instances for which no specific name was given, so: > > # x.name.[0] <- '!';; > - : unit = () > # default_datarec.name;; > - : string = "!" > What about having a way to initialise struct types : something like : type datarec = { mutable name : string = "?"; mutable color : string = "?"; mutable value : int = 0; } And then you could do : let x : datarec = { value = 113 } or maybe let x = { type datarec with value = 113 } or something such ? This would permit to do as above, but without the sharing problem. But is it really usefull, i guess you could simply wrap the datatype in a constructor, and not worry about such things : type datarec = { mutable name : string; mutable color : string; mutable value : int; } let new_datarec n c v = { name = match n with None -> "?" | Some n -> n; color = match c with None -> "?" | Some c -> c; value = match v with None -> 0 | Some v -> v; } Then you could simply do : let x = new_datarec None None (Some 113) let y = new_datarec (Some "y") ("00FF00 FF00FF") None Or even something more cleaner using labels and optional values. Friendly, Sven Luther ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Caml-list] Newbie: declarations 2001-06-19 10:22 ` Sven LUTHER @ 2001-06-19 12:25 ` Frank Atanassow 0 siblings, 0 replies; 12+ messages in thread From: Frank Atanassow @ 2001-06-19 12:25 UTC (permalink / raw) To: Sven LUTHER; +Cc: Fr?d?ric van der Plancke, caml Sven LUTHER wrote (on 19-06-01 12:22 +0200): > What about having a way to initialise struct types : > > something like : > > type datarec = { > mutable name : string = "?"; > mutable color : string = "?"; > mutable value : int = 0; > } > [...] > But is it really usefull, i guess you could simply wrap the datatype in a > constructor, and not worry about such things : Yes, and it also doesn't make sense for the common case where fields are polymorphic. > type datarec = { > mutable name : string; > mutable color : string; > mutable value : int; > } > > let new_datarec n c v = { > name = match n with None -> "?" | Some n -> n; > color = match c with None -> "?" | Some c -> c; > value = match v with None -> 0 | Some v -> v; > } > > Then you could simply do : > > let x = new_datarec None None (Some 113) > let y = new_datarec (Some "y") ("00FF00 FF00FF") None There is a simpler way: let new_datarec = { name = "?"; color = "?"; value = 0 } let x = { new_datarec with value = 113 } let y = { new datarec with color = "00FF00 FF00FF"; name = "y" } -- Frank Atanassow, Information & Computing Sciences, Utrecht University Padualaan 14, PO Box 80.089, 3508 TB Utrecht, Netherlands Tel +31 (030) 253-3261 Fax +31 (030) 251-379 ------------------- 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2001-06-19 12:25 UTC | newest] Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2001-06-17 0:09 [Caml-list] Newbie: declarations leary 2001-06-17 10:08 ` Vitaly Lugovsky 2001-06-18 2:19 ` leary 2001-06-18 15:18 ` Remi VANICAT 2001-06-18 20:19 ` [Caml-list] Good introduction for the working programmer in Objective CAML Mattias Waldau 2001-06-18 20:35 ` Miles Egan 2001-06-18 20:49 ` leary 2001-06-17 15:33 ` [Caml-list] Newbie: declarations David Fox 2001-06-18 14:52 ` FabienFleutot 2001-06-19 8:11 ` Frédéric van der Plancke 2001-06-19 10:22 ` Sven LUTHER 2001-06-19 12:25 ` Frank Atanassow
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox