caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Problem in modifying a mutable element in an array
@ 2003-02-15 22:47 WatchDog
  2003-02-15 23:07 ` Samuel Mimram
  0 siblings, 1 reply; 3+ messages in thread
From: WatchDog @ 2003-02-15 22:47 UTC (permalink / raw)
  To: caml-list, pierre.weis

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Problem in modifying a mutable element in an array
  2003-02-15 22:47 [Caml-list] Problem in modifying a mutable element in an array WatchDog
@ 2003-02-15 23:07 ` Samuel Mimram
  2003-02-17  3:29   ` Issac Trotts
  0 siblings, 1 reply; 3+ messages in thread
From: Samuel Mimram @ 2003-02-15 23:07 UTC (permalink / raw)
  To: WatchDog; +Cc: caml-list

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

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 <watchdog@free.fr> 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


-- 
Samuel Mimram

Samuel.Mimram@ens-lyon.fr
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Problem in modifying a mutable element in an array
  2003-02-15 23:07 ` Samuel Mimram
@ 2003-02-17  3:29   ` Issac Trotts
  0 siblings, 0 replies; 3+ messages in thread
From: Issac Trotts @ 2003-02-17  3:29 UTC (permalink / raw)
  To: OCaml List

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 <watchdog@free.fr> 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-02-17  3:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-15 22:47 [Caml-list] Problem in modifying a mutable element in an array WatchDog
2003-02-15 23:07 ` Samuel Mimram
2003-02-17  3:29   ` Issac Trotts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).