Appel initial : Le premier appel de la fonction.
Appel initial : Le premier appel de la fonction.
Appel initial : Le premier appel de la fonction.
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Exemple complémentaire:<br />
Fonction affiche_int qui affiche les n <strong>premier</strong>s entiers > 0 sur une ligne puis passe à <strong>la</strong> ligne.<br />
Récursivité c<strong>la</strong>ssique<br />
let affiche_int n =<br />
let rec affiche n =<br />
if n=0 then []<br />
else print_int n::affiche (n-1)<br />
in<br />
print_newline ()::affiche n;;<br />
val affiche_int : int -> unit list = <br />
affiche_int 4;;<br />
1234<br />
- : unit = ()<br />
Remarquez l'ordre dans lequel les entiers sont affichés. <strong>Le</strong> <strong>premier</strong> affichage est réalisé quand on a<br />
rencontré le cas simple. <strong>Le</strong> <strong>premier</strong> entier affiché sera donc 1.<br />
Récursivité terminale<br />
let affiche_int n =<br />
let rec affiche acc u n =<br />
if acc>n then print_newline ()<br />
else affiche (acc+1) (print_int acc) n<br />
in<br />
affiche 1 () n;;<br />
val affiche_int : int -> unit list = <br />
affiche_int 4;;<br />
1234<br />
- : unit = ()<br />
L'accumu<strong>la</strong>teur n'est pas utilisé. On peut aussi écrire :<br />
let rec affiche_int n =<br />
if n=0 then print_newline ()<br />
else<br />
begin<br />
print_int n;affiche_int (n-1)<br />
end;;<br />
affiche_int 4;;<br />
4321<br />
- : unit = ()