caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Instruction return
@ 1997-06-02 16:23 Dwight VandenBerghe
  1997-06-02 21:41 ` Pierre Weis
  0 siblings, 1 reply; 7+ messages in thread
From: Dwight VandenBerghe @ 1997-06-02 16:23 UTC (permalink / raw)
  To: Pascal Zimmer, caml-list

(I apologize for no French version)

> let rech x v =
>  let n = vect_length v in
>  for i=0 to n-1 do
>   if v.(i) = x then return true
>  done; false;;

This is one of the two main control problems with functional programming,
IMHO.
The solution I use is:

	exception Found_it   
	let rech x v =
	    try
	        for i = 0 to pred (vect_length v) do
	            if v.(i) = x then raise Found_it
	            done;
	        false
	    with Found_it -> true
	    
You don't have to assign vect_length v to n; it is only evaluated once
anyway.
If iter is defined on the type of v, then you can also use

	let rech x v =
	    try
	        let f x1 = if x1 = x then raise Found_it in
	        iter f v;  false
	    with Found_it -> true
 
The other control problem is when you want the equivalent of:

    x = foo(a);
    bar();
    return x;

If you try 

    let x = foo(a) in
    bar();
    x

then bar gets done first, instead of second.  I use this instead:

    let f x = bar(); x in	
    in f (foo a)

I haven't found any other serious problems with Caml's logic flow.  These
two just take some getting used to.

Dwight





^ permalink raw reply	[flat|nested] 7+ messages in thread
* Instruction return
@ 1997-05-31 17:30 Pascal Zimmer
  1997-06-02 14:15 ` Pierre Weis
  0 siblings, 1 reply; 7+ messages in thread
From: Pascal Zimmer @ 1997-05-31 17:30 UTC (permalink / raw)
  To: caml-list

Serait-il possible d'ajouter une instruction return au langage CAML afin 
de pouvoir sortir directement d'une boucle, comme c'est le cas en C ? Je 
pense qu'une telle instruction ameliorerait beaucoup la lisibilite du 
code.

Par exemple, cela permettrait de retourner la valeur d'une fonction au 
cours d'une boucle for, alors que pour l'instant on doit utiliser une 
boucle while avec un compteur sous forme de pointeur.

Ainsi, une fonction classique de recherche d'une valeur x dans un tableau 
non trie v s'ecrit:

let rech x v =
 let n = vect_length v in
 let i = ref 0 in
 while !i < n & v.(!i) <> x do i := !i + 1 done;
 not (!i = n);;

Avec une instruction return, on pourrait l'ecrire ainsi:

let rech x v =
 let n = vect_length v in
 for i=0 to n-1 do
  if v.(i) = x then return true
 done; false;;

Cette derniere forme sans pointeur pour le compteur i est tout de meme 
plus claire !

Bien entendu, le typeur devra verifier que le type renvoye par return est 
le meme que celui renvoye par la derniere instruction de la fonction.
Une telle possibilite (renvoi par return ou par la derniere instruction) 
est par exemple utilisee par le langage de programmation de MAPLE, sauf 
qu'il n'y a aucun controle de type.

J'aimerais donc savoir si une telle instruction est possible ou s'il y a 
incompatibilite avec le reste du langage.

[English: Is it possible to add a 'return' instruction to the CAML 
language, like in C ?]







^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1997-06-03 15:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-02 16:23 Instruction return Dwight VandenBerghe
1997-06-02 21:41 ` Pierre Weis
  -- strict thread matches above, loose matches on Subject: below --
1997-05-31 17:30 Pascal Zimmer
1997-06-02 14:15 ` Pierre Weis
1997-06-03  8:37   ` Vincent Poirriez
1997-06-03 10:24     ` Pierre Weis
1997-06-03 14:17       ` Vincent Poirriez

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).