01.06.2013 Views

Appel initial : Le premier appel de la fonction.

Appel initial : Le premier appel de la fonction.

Appel initial : Le premier appel de la fonction.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Exercice 1<br />

1°/ Ecrire une <strong>fonction</strong> récursive qui permet <strong>de</strong> rentrer au c<strong>la</strong>vier, les éléments d'une liste<br />

d'entiers positifs ou nuls, <strong>la</strong> saisie s'arrête quand <strong>la</strong> valeur entrée est négative.<br />

a. Création d'une nouvelle liste<br />

Récursivité c<strong>la</strong>ssique :<br />

let rec lire_int_list () =<br />

let x = read_int() in<br />

if x int list = <br />

L'absence d'argument d'une <strong>fonction</strong> doit être représenté par les parenthèses () dans l'en-ête <strong>de</strong> <strong>la</strong><br />

<strong>fonction</strong> et lors <strong>de</strong> l'<strong>appel</strong> <strong>de</strong> <strong>la</strong> <strong>fonction</strong>.<br />

lire_int_list;;<br />

- : unit -> int list = <br />

lire_int_list ();;<br />

1<br />

2<br />

3<br />

-1<br />

- : int list = [1; 2; 3]<br />

Récursivité terminale :<br />

let lire_int_list () =<br />

let rec aux accumu<strong>la</strong>teur =<br />

let x = read_int() in<br />

if x

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!