Corrig´ e de la Composition d’Informatique
Les Principes des Langages de Programmation (INF 321) Promotion 2009
Sujet propos´e par Gilles Dowek 13 juillet 2010
Exercice 1
1. L’ensemble {0,1,3}.
2. Ce mˆeme ensemble.
3.
static List inter(List l1,List l2) { if (l1 == null) return null;
if (l2 == null) return null;
return new List(l1.hd && l2.hd,inter(l1.tl,l2.tl));}
4.
static List union(List l1,List l2) { if (l1 == null) return l2;
if (l2 == null) return l1;
return new List(l1.hd || l2.hd,union(l1.tl,l2.tl));}
5.
static boolean subset(List l1,List l2) { if (l1 == null) return true;
if (l2 == null) return (l1.hd == false) && subset(l1.tl,null);
return ((l1.hd == false) || (l2.hd == true)) && subset(l1.tl,l2.tl);}
Exercice 2
1. La longueur de la liste.
2.
int longueur () {
if (tl == null) return 1;
return 1 + tl.longueur();}
3. La liste vide.
Exercice 3
1. Le produit des contenus de tous les nœuds de l’arbre.
2.
static int produit2 (Arbre a) { int n, p;
if (a == null) return 1;
if (a.val == 0) return 0;
n = produit2(a.gauche);
if (n == 0) return 0;
p = produit2(a.droite);
return a.val * n * p;}
Exercice 4
1. Le corps de la boucle n’est jamais ex´ecut´e.
2. Si a <= 100 alorsa’ = a + 11 et b’ = b + 1, doncb’ >= 0 eta’ - 10 b’ = a - 10 b + 1 <= a + 1 <= 101. Sia > 100alorsb > 0,a’ = a - 10etb’ = b - 1, doncb’ >= 0eta’ - 10 b’ = a - 10 b <= 101.
Ces deux propri´et´es sont v´erifi´ees au d´ebut de l’ex´ecution de la boucle (a <= 100et b = 0), donc elles sont v´erifi´ees dans tous les ´etats produits au cours de l’ex´ecution de la boucle.
3.2a - 21b = 2 (a - 10 b) - b <= 2(a - 10 b) <= 202.
4. Sia <= 100alorsa’ = a + 11etb’ = b + 1, donc2a’ - 21 b’ = 2a - 21b + 1. Sia > 100alorsa’ = a - 10etb’ = b - 1, donc2a’ - 21 b’ = 2a - 21b + 1.
La boucle termine car cette quantit´e ne peut pas croˆıtre ind´efiniment tout en restant inf´erieure ou ´egale `a 202.
5. On ab >= 0eta - 10 b <= 101. Par ailleurs, comme la condition de maintien dans la boucle n’est plus v´erifi´ee on a a > 100et b <= 0. On en d´eduit b = 0 et 100 < a <=
101, soita = 101.
6.
static int f (int x) { if (x <= 100) return 101;
return x;}
Exercice 5
1.0.
2.1.
2