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 PAA12311; Mon, 11 Jun 2001 15:43:00 +0200 (MET DST) 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 PAA11963 for ; Mon, 11 Jun 2001 15:42:59 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f5BDgwD03380; Mon, 11 Jun 2001 15:42:58 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA12420; Mon, 11 Jun 2001 15:42:58 +0200 (MET DST) From: Pierre Weis Message-Id: <200106111342.PAA12420@pauillac.inria.fr> Subject: Re: [Caml-list] let mutable (was OCaml Speed for Block Convolutions) In-Reply-To: <01060821302901.00670@haguenauer> from Michel Quercia at "Jun 8, 101 09:30:29 pm" To: michel.quercia@prepas.org Date: Mon, 11 Jun 2001 15:42:58 +0200 (MET DST) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] 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 > Le Vendredi 8 Juin 2001 19:30, Pierre Weis a écrit : > > > The introduction of a ``let mutable'', more concisely noted with the > > var keyword, is not new: it has been discussed in the Caml groups 3 or > > 4 years ago. We chose to abandon it for sake of semantics simplicity > > of the language. > > For beginners (f.e. students) things look a bit complicated : > > (* summing up all elements of an integer array *) > let adda a = > let res = ref 0 in > let i = ref 0 in > while !i < Array.length(a) do res := !res+a.(!i); i := !i+1 done; > !res > ;; > > A lot of boring exclam, but that's the price to pay for having > mutable values, and that's logical. Okay ... > > (* 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 ? The for loop is a short hand for a call to a local recursive function: no reference and no problem here, unless you consider that you cannot change the arguments of a recursive call to a function. (For readers not familiar with the subject, let's recall that for i = e1 to e2 do e3 done is equivalent to let rec _loop i _lim = if i <= _lim then begin e3; _loop (i + 1) _lim end in _loop e1 e2 (where _loop and _lim stand for new fresh identifiers, not free in e1, e2, or e3) ) > > 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". I'm afraid this is wrong. The syntactic construction e1.(e2) <- e3 is a short hand for a function call: Array.set e1 e2 e3. Once more there is no Lvalue here, just a regular function call (hence you can write arbitrary complex expressions in place of e1, provided it returns an array value). I'm a bit surprised that you feel it necessary to explain the notion of Lvalue to beginners when there is no such notion in the language ! Best regards. Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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