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 QAA21342; Fri, 28 Dec 2001 16:37:18 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA20903 for ; Fri, 28 Dec 2001 16:37:17 +0100 (MET) Received: from mel-rto2.wanadoo.fr (smtp-out-2.wanadoo.fr [193.252.19.254]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id fBSFbGT10584 for ; Fri, 28 Dec 2001 16:37:16 +0100 (MET) Received: from mel-rta7.wanadoo.fr (193.252.19.61) by mel-rto2.wanadoo.fr; 28 Dec 2001 16:37:15 +0100 Received: from debian.castelnau.fr (193.248.49.242) by mel-rta7.wanadoo.fr; 28 Dec 2001 16:36:57 +0100 Received: from hubert by debian.castelnau.fr with local (Exim 3.32 #1 (Debian)) id 16Jz3B-0006GJ-00; Fri, 28 Dec 2001 16:36:01 +0100 To: caml Subject: Re: [Caml-list] Question sur les références. References: <3C2C7FC9.86AD1CA3@free.fr> From: hubert.fauque@wanadoo.fr Date: 28 Dec 2001 16:36:01 +0100 In-Reply-To: <3C2C7FC9.86AD1CA3@free.fr> Message-ID: <87vger1hni.fsf@debian.castelnau.fr> User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk English summary follows WatchDog writes: > Bonjour ! Cette petite fonction me pose des problèmes : > > let resultat (a,b,c,d) position = > let pos=ref position in > !pos.(d).(c) <- !pos.(b).(a) ; !pos.(b).(a) <- V ; > !pos ;; > > Quel ne fut pas mon étonnement en constatant que cette fonction se > permettait de modifier le contenu même de position, alors que je ne l'ai > utilisé qu'avec une référence (pos) qui pointait dessus ! > Quand on écrit > let x = ref 1 in x := !x + 1;; > ça ne modifie pas la valeur de l'entier 1, non ??? > Merci de votre éclairage. pos est une référence sur position ce qui veut dire que !pos est égal à position; donc faire !pos.(d).(c) <- !pos.(b).(a) ; !pos.(b).(a) <- V ; est identique à faire position.(d).(c) <- position.(b).(a) ; position.(b).(a) <- V ; ce que vous voulez faire en fait est une copie du tableau ce qui se fait pour un tableau simple avec Array.copy, mais comme ici c'est une matrice faire Array.copy ne copiera qu'un niveau de la structure du tableau; il faut donc faire: let pos = Array.map Array.copy position in pos.(d).(c) <- pos.(b).(a) ; pos.(b).(a) <- V ; taking a ref on an array does not create a new array; it is necessary to use Array.copy and in case of a matrix (Array.map Array.copy) Hubert Fauque ------------------- 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