caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Vincent Poirriez <Vincent.Poirriez@univ-valenciennes.fr>
To: Pierre Weis <Pierre.Weis@inria.fr>
Cc: Pascal Zimmer <zimmer@easynet.fr>, caml-list@inria.fr
Subject: Re: Instruction return
Date: Tue, 03 Jun 1997 09:37:40 +0100	[thread overview]
Message-ID: <3393D7D4.2F33@univ-valenciennes.fr> (raw)
In-Reply-To: <199706021415.QAA21013@pauillac.inria.fr>

> 
> > Serait-il possible d'ajouter une instruction return au langage CAML afin
> > de pouvoir sortir directement d'une boucle, 
> ...

Je souscrit a la réponse de Pierre Weis

>C'est pourquoi on pre'fe`re alors conside'rer que la
>boucle est trop complexe pour une boucle for ou while et qu'il est
>temps d'utiliser des outils adapte's a` e'crire des boucles
>arbitrairement complexes, c'est-a`-dire les fonctions re'cursives.

Cependant, j'ai rencontré la situation suivante dans un contexte
où l'efficacité (à l'exécution) est essentielle.


Trouver l'indice du premier élément d'un tableau qui vérifie
un prédicat p, s'il y en a un. 

Ce code étant compile avec l'option -unsafe, je ne peux pas
compter sur les test de débordement des primitives Array.get ...

Le choix d'utiliser Array.unsafe_get serait inutile si j'inclus
ce test de débordement dans la condition 
du while ou de la fonction récursive ainsi:

 let find p a =
  let l = Array.length a in
  let i = ref 0 in
  while !i<l & not( p (Array.unsafe_get a !i) do
  incr i
  done;
  !i

J'ai donc prefere le coût d'un try ... with:

exception Exit_for of int
 let find p a =
 let l = Array.length a in
  try
   for i = 0 to l-1 do
    if p (Array.unsafe_get a i) then raise Exit_for i
   done;
   l
  with Exit_for i -> i

Si l'on veut sortir d'une boucle for avant sa fin normale, en
admettant que ce soit justifié (while ou fonction récursive exclue)
, la valeur de sortie du compteur suffit.

English short version:

I agree with Pierre Weis:
>In fact the best way of writing complex loops is via recursive
>function definitions:

But, due to runtime efficiency considerations I had to make
the following choice.

 I want to find the index of the first item of an array which
verifies a predicat p, if one exists. 

It is useless to prefer Array.unsafe_get if I add the test
in the while (or recursive) condition as below:

let find p a =
  let l = Array.length a in
  let i = ref 0 in
  while !i<l & not( p (Array.unsafe_get a !i) do
  incr i
  done;
  !i

I prefer to pay the cost of one try ... with ...


exception Exit_for of int
 let find p a =
 let l = Array.length a in
  try
   for i = 0 to l-1 do
    if p (Array.unsafe_get a i) then raise Exit_for i
   done;
   l
  with Exit_for i -> i

When I want to exit a loop for before its natural end, when I really
need it, it is sufficient to return the value of the count
when I exit

Vincent Poirriez
-- 
Tel: (33) {0}3 27 14 13 33   Fax: (33) {0}3 27 14 12 94
mailto:Vincent.Poirriez@univ-valenciennes.fr
 http://www.univ-valenciennes.fr/limav/poirriez
ISTV Université de Valenciennes Le Mont Houy BP 311 F59304 Valenciennes
CEDEX





  reply	other threads:[~1997-06-03 10:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-05-31 17:30 Pascal Zimmer
1997-06-02 14:15 ` Pierre Weis
1997-06-03  8:37   ` Vincent Poirriez [this message]
1997-06-03 10:24     ` Pierre Weis
1997-06-03 14:17       ` Vincent Poirriez
1997-06-02 16:23 Dwight VandenBerghe
1997-06-02 21:41 ` Pierre Weis

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=3393D7D4.2F33@univ-valenciennes.fr \
    --to=vincent.poirriez@univ-valenciennes.fr \
    --cc=Pierre.Weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=zimmer@easynet.fr \
    /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).