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 IAA03347; Mon, 11 Jun 2001 08:42:53 +0200 (MET DST) 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 IAA03334 for ; Mon, 11 Jun 2001 08:42:52 +0200 (MET DST) Received: from ext.lri.fr (ext.lri.fr [129.175.15.4]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f5B6gpv06875 for ; Mon, 11 Jun 2001 08:42:51 +0200 (MET DST) Received: from pc87 (pc87 [129.175.8.106]) by ext.lri.fr (8.11.1/jtpda-5.3.2) with SMTP id f5B6gn510984 ; Mon, 11 Jun 2001 08:42:49 +0200 (MET DST) Date: Mon, 11 Jun 2001 08:42:49 +0200 From: Judicaël Courant To: michel.quercia@prepas.org Cc: caml-list@inria.fr Subject: Re: [Caml-list] should "a.(i)" be a reference? (was "let mutable") Message-Id: <20010611084249.01544d7b.Judicael.Courant@lri.fr> In-Reply-To: <01060821302901.00670@haguenauer> References: <200106081730.TAA27320@pauillac.inria.fr> <01060821302901.00670@haguenauer> X-Mailer: Sylpheed version 0.4.64 (GTK+ 1.2.7; i386-debian-linux-gnu) Organization: LRI 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 Hi, On Fri, 8 Jun 2001 21:30:29 +0200 Michel Quercia wrote: > (* same, but with a for loop *) > let add_1 a = > let res = ref 0 in > for i=0 to Array.length(a)-1 do res := !res + a.(i) done; > !res > ;; > > No exclam and no ref for i ? And its value is changing though ? Where is > gone the logic ? It does not change more than in "List.iter (fun i -> res := !res + a(i)) l". The "for" loop is just a new kind of binder. With the suitable for_loop function (let rec for_loop b e f = if b <= e then begin f b; for_loop (b+1) e f end), you could write this as: for_loop 0 (Array.length(a)-1) (fun i -> res := !res + a.(i)) On the contrary, a mutable "i" would mean you could change the way the iterations are done inside your loop, which is not really nice IMHO: for i:= 0 to Array.length(a)-1 do res := !res+a.(!i); if once_again() then i := !i - 1 done is really bad programming style; if you want to do this, use a while loop instead. > > > This construction would have introduced the notion of > > Lvalue in Caml, thus introducing some additional semantics complexity, > > and a new notion to explain to beginners. > > Lvalues already exist in Ocaml (and have to be explained to beginners), for > example : "a.(i) <- a.(i)+1". > You are right, but maybe this suggest that a.(i) should be typed as a reference? Or, in order to preserve compatibility, the reference could be noted "a.&(i)"; "a.(i)" would be just syntactic sugar for "!a.&(i)" and "a.(i) <- y" would be syntactic sugar for "a.&(i) := y". The same could be done for mutable record fields. This would not only tidy the semantics, but it would also enrich the language: the programmer could then use references to mutable record fields or to array cells as function arguments (would anybody be interested by such a feature?). Best regards, Judicaël Courant. -- Judicael.Courant@lri.fr, http://www.lri.fr/~jcourant/ (+33) (0)1 69 15 64 85 ------------------- 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