Appel initial : Le premier appel de la fonction.
Appel initial : Le premier appel de la fonction.
Appel initial : Le premier appel de la fonction.
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Calculez <strong>la</strong> somme <strong>de</strong>s entiers <strong>de</strong> <strong>la</strong> liste [9;8;10;7;5;2]<br />
calcul [9;8;10;7;5;2] (+) 0;;<br />
- : int = 41<br />
Concaténez les chaînes <strong>de</strong> caractères <strong>de</strong> <strong>la</strong> liste ["un ";"<strong>de</strong>ux ";"trois "]<br />
calcul_terminal ["un ";"<strong>de</strong>ux ";"trois "] (^) "";;<br />
- : string = "trois <strong>de</strong>ux un "<br />
calcul_terminal (inverse ["un ";"<strong>de</strong>ux ";"trois "]) (^) "";;<br />
- : string = "un <strong>de</strong>ux trois "<br />
• Ecrire <strong>la</strong> <strong>fonction</strong> récursive terminale calcul_conditionnel_terminal qui applique <strong>la</strong><br />
<strong>fonction</strong> f aux éléments d'une liste qui respecte le prédicat p.<br />
let rec calcul_conditionnel_terminal liste f p accumu<strong>la</strong>teur =<br />
match liste with<br />
|[] -> accumu<strong>la</strong>teur<br />
|h::t -> if p h then calcul_conditionnel_terminal (f h accumu<strong>la</strong>teur) t f p<br />
else calcul_conditionnel_terminal accumu<strong>la</strong>teur t f p;;<br />
val calcul_conditionnel _terminal:<br />
'a -> 'b list -> ('b -> 'a -> 'a) -> ('b -> bool) -> 'a = <br />
• Ecrire <strong>la</strong> <strong>fonction</strong> récursive terminale calcul_conditionnel qui applique <strong>la</strong> <strong>fonction</strong> f aux<br />
éléments d'une liste qui respecte le prédicat p.<br />
let rec calcul_conditionnel liste f p resultat_simple =<br />
match liste with<br />
|[] -> resultat_simple<br />
|h::t -> if p h then f h (calcul_conditionnel t f p resultat_simple)<br />
else calcul_conditionnel t f p resultat_simple;;<br />
val calcul_conditionnel : 'a -> 'b list -> ('b -> 'a -> 'a) -> ('b -> bool) -> 'a = <br />
Calculez <strong>la</strong> somme <strong>de</strong>s entiers pairs <strong>de</strong> <strong>la</strong> liste [9;8;10;7;5;2]<br />
calcul_conditionnel 0 [9;8;10;7;5;2] (+) (fun x -> x mod 2 = 0);;<br />
Ecrire une <strong>fonction</strong> entiers_positifs qui construit une liste constituée <strong>de</strong>s entiers positifs<br />
présents dans une liste d'entiers.<br />
let entiers_positifs liste =<br />
calcul_conditionnel liste (fun h liste -> h::liste) (fun x -> x>=0) [];;<br />
let entiers_positifs liste =<br />
let positif x = x>=0 in<br />
let insere h liste = h::liste in<br />
calcul_conditionnel liste insere positif [];;