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 EAA19687; Mon, 17 Feb 2003 04:23:43 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id EAA19681 for ; Mon, 17 Feb 2003 04:23:42 +0100 (MET) Received: from marble.he.net (marble.he.net [216.218.230.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h1H3NeP24190 for ; Mon, 17 Feb 2003 04:23:41 +0100 (MET) Received: from ucdavis.edu ([208.227.214.58] (may be forged)) by marble.he.net (8.8.6/8.8.2) with ESMTP id TAA09561 for ; Sun, 16 Feb 2003 19:23:36 -0800 Message-ID: <3E505701.4010300@ucdavis.edu> Date: Sun, 16 Feb 2003 19:29:05 -0800 From: Issac Trotts User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020913 Debian/1.1-1 MIME-Version: 1.0 To: OCaml List Subject: Re: [Caml-list] Problem in modifying a mutable element in an array References: <3E4EC399.20200@free.fr> <20030216000742.3376f3bc.Samuel.Mimram@ens-lyon.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Samuel Mimram wrote: >Hello, > >Your "problem" is due to the fact that when you use the function make_vect m agent_init, Caml creates an array where all element are pointing at the same object which is agent_init : in your whole program, there is only one instance of agent_init in memory. > >I think that the only solution is to make the initilization of f by hand : > >let agent_init () = { initial = 0 ; courant = 0 ; longueur = 0.; liste_parcourue = [] ; à_visiter = make_vect n 1 } ;; > >let f = make_vect m (agent_init ());; > >for i = 0 to ((vect_length f)-1) >do >f.(i) <- agent_init () >done > How about let f = Array.init m (fun _ -> { initial = 0 ; courant = 0 ; longueur = 0.; liste_parcourue = [] ; à_visiter = make_vect n 1 }) Issac >The use of a function which takes a unit argument is important because each time you call it, it returns a NEW record. > >I hope this helps. > >Samuel. > > >On Sat, 15 Feb 2003 23:47:53 +0100 >WatchDog wrote: > > > >>Hello ! >>My problem has first appeared in a CamlLight implementation, but I have >>then tried to do the same on O'Caml and the problem remained equal. But >>I'm sure you'll be able to provide some help, won't you ? >>In Caml Light : >> >>#open "random";; >>type agent = { mutable initial : int ; mutable courant : int ; >> mutable longueur : float ; mutable liste_parcourue : int >>list ; >> à_visiter : int vect } ;; >>let agent_init = { initial = 0 ; courant = 0 ; longueur = 0. ; >>liste_parcourue = [] ; >> à_visiter = make_vect n 1 } ;; >>let f = make_vect m agent_init ;; >> >>let set_f () = for p = 0 to (m-1) do >> begin >> f.(p).initial <- (int n) ; >> f.(p).courant <- (int n) ; >> f.(p).liste_parcourue <- [(int n)] ; >> f.(p).à_visiter.(int n) <- 0 >> end >>done >>;; >> >> >>And then set_f() doesn't work properly : it seems Caml refuses to modify >>one element (for instance f.(5).initial) without modifying automatically >>all its brothers, e.g. all the f.(i).initial ; for i from 0 to (m-1). >>The fact is that I'd like to modify it element-by-element, and not all >>at the same time. >> >>Your help would be a real relief !! >>Thanks >> >>JB, from Paris. >> >>------------------- >>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 >> >> > > > > ------------------- 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