From: Judicaël Courant <Judicael.Courant@lri.fr>
To: michel.quercia@prepas.org
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] should "a.(i)" be a reference? (was "let mutable")
Date: Mon, 11 Jun 2001 08:42:49 +0200 [thread overview]
Message-ID: <20010611084249.01544d7b.Judicael.Courant@lri.fr> (raw)
In-Reply-To: <01060821302901.00670@haguenauer>
Hi,
On Fri, 8 Jun 2001 21:30:29 +0200
Michel Quercia <michel.quercia@prepas.org> 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
next prev parent reply other threads:[~2001-06-11 6:42 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-04 13:25 [Caml-list] OCaml Speed for Block Convolutions David McClain
2001-06-04 19:51 ` William Chesters
2001-06-04 20:05 ` Chris Hecker
2001-06-04 20:15 ` David McClain
2001-06-04 22:34 ` Markus Mottl
2001-06-06 20:13 ` William Chesters
2001-06-06 22:29 ` Chris Hecker
2001-06-07 7:42 ` William Chesters
2001-06-05 7:22 ` Chris Hecker
2001-06-06 6:27 ` David McClain
2001-06-04 22:14 ` Tom _
2001-06-04 22:57 ` Chris Hecker
2001-06-05 2:52 ` Brian Rogoff
2001-06-05 15:02 ` Stefan Monnier
2001-06-05 10:48 ` Tom _
2001-06-06 2:03 ` Hugo Herbelin
2001-06-06 4:04 ` Charles Martin
2001-06-06 18:25 ` William Chesters
2001-06-06 18:35 ` William Chesters
2001-06-06 18:40 ` Patrick M Doane
2001-06-07 1:50 ` Hugo Herbelin
2001-06-07 18:20 ` Tom _
2001-06-07 23:49 ` [Caml-list] let mutable (was OCaml Speed for Block Convolutions) Jacques Garrigue
2001-06-08 0:20 ` [Caml-list] Currying in Ocaml Mark Wotton
2001-06-08 10:13 ` Anton Moscal
[not found] ` <Pine.LNX.4.21.0106081015000.1167-100000@hons.cs.usyd.edu.a u>
2001-06-08 0:38 ` Chris Hecker
2001-06-08 8:25 ` [Caml-list] let mutable (was OCaml Speed for Block Convolutions) Ohad Rodeh
2001-06-08 15:21 ` Brian Rogoff
2001-06-08 17:30 ` Pierre Weis
2001-06-08 18:36 ` Stefan Monnier
2001-06-08 19:07 ` Pierre Weis
2001-06-08 19:30 ` Michel Quercia
2001-06-11 6:42 ` Judicaël Courant [this message]
2001-06-11 13:42 ` Pierre Weis
2001-06-12 3:21 ` Jacques Garrigue
2001-06-12 7:43 ` Pierre Weis
2001-06-12 8:31 ` Jacques Garrigue
2001-06-12 13:15 ` Georges Brun-Cottan
2001-06-12 21:54 ` John Max Skaller
2001-06-15 9:55 ` Michael Sperber [Mr. Preprocessor]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20010611084249.01544d7b.Judicael.Courant@lri.fr \
--to=judicael.courant@lri.fr \
--cc=caml-list@inria.fr \
--cc=michel.quercia@prepas.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).