(* Types des coefficients *) type 'a dico = {zero: 'a ; un : 'a ; is_zero : 'a -> bool ; plus : 'a -> 'a -> 'a ; n_plus : int -> 'a -> 'a ; mult : 'a -> 'a -> 'a ; print : 'a -> unit} ;; (* Qui sont ici des flottants *) let dico_float = {zero=0.0 ; un=1.0 ; is_zero=(fun x -> x = 0.0) ; plus=(fun x y -> x +. y) ; n_plus=(fun n x -> float_of_int n *. x) ; (* itérer plus n fois *) mult=(fun x y -> x *. y) ; print=print_float} ;; |
let rec somme dico p1 p2 = match p1,p2 with | [],_ -> p2 | _,[] -> p1 | ({degre=d1} as m1)::r1, ({degre=d2} as m2)::r2 -> if d1 > d2 then m1::somme dico r1 p2 else if d1 < d2 then m2::somme dico p1 r2 else (* d1 = d2 *) let c = dico. plus m1.coeff m2.coeff in if dico.is_zero c then somme dico r1 r2 else {coeff=c ; degre=d1}::somme dico r1 r2 ;; |