IF242 Contrôle TP 27/05/2002
Petite mise en jambes
exercice 1
# let rec loop p f x = if (p x)
then
x
else
loop p f (f x);;
val loop : ('a -> bool) -> ('a -> 'a) -> 'a -> 'a = <fun>
exercice 2
# let rec exists p l = match l with
| [] -> false
| u::l' -> (p u) || exists
p l';;
val exists : ('a -> bool) -> 'a list -> bool = <fun>
exercice 3
# let rec find p l = match l with
u::l' -> if (p u)
then u
else find p l';;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
val find : ('a -> bool) -> 'a list -> 'a = <fun>
Arbres planaires généraux
# type 'a arbre = Vide | Noeud of 'a * 'a arbre list;;
type 'a arbre = Vide | Noeud of 'a * 'a arbre list
exercice 4
# let rec max_l l = match l with
| [u] -> u
| u::l' -> max u (max_l
l');;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
val max_l : 'a list -> 'a = <fun>
# let rec hauteur a = match a with
| Vide -> 0
| Noeud (_, l) -> 1 + max_l
(map hauteur l);;
val hauteur : 'a arbre -> int = <fun>
exercice 5
# let rec map_arbre f a = match a with
| Vide -> Vide
| Noeud (n, l) -> Noeud
(f n, map (map_arbre f) l);;
val map_arbre : ('a -> 'b) -> 'a arbre -> 'b arbre = <fun>
exercice 6
# let rec exists_arbre p a = match a with
| Vide -> false
| Noeud (n, l) -> (p n)
|| exists (exists_arbre p) l;;
val exists_arbre : ('a -> bool) -> 'a arbre -> bool = <fun>
exercice 7
# let element x a = exists_arbre (fun e -> (e=x)) a;;
val element : 'a -> 'a arbre -> bool = <fun>
Home Up