Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let pr = Printf.printf
- (** Question 1 : Méthode bourrine *)
- class fibo =
- object(self)
- method compute n =
- pr "I want to compute fibo(%d) !\n" n;
- match n with
- 0 -> 0
- | 1 -> 1
- | _ -> (self#compute (n-1)) + (self#compute (n-2))
- end
- let _ =
- pr "Méthode bourrine\n";
- let f = new fibo in
- f#compute 5
- (** Question 2 : (m)némo le petit poisson qui avait de la mémoire *)
- module type T = sig
- class c : object
- method compute : int -> int
- end
- end
- module Memo (M:T) : T = struct
- class c =
- object(self)
- inherit M.c as poisson_rouge
- val table = Hashtbl.create 100
- method compute n =
- try
- Hashtbl.find table n
- with Not_found ->
- let r = poisson_rouge#compute n in
- Hashtbl.add table n r ;
- r
- end
- end
- module MyT = struct
- class c = fibo
- end
- let _ =
- pr "Méthode (M)némo\n";
- let module Memo_fibo = Memo(MyT) in
- let f2 = new Memo_fibo.c in
- f2#compute 5
- (** Question 3 : Vive les sushis ! *)
- class fibo =
- object(self)
- method compute n =
- pr "I want to compute fibo(%d) !\n" n;
- match n with
- 0 -> 0
- | 1 -> 1
- | _ -> (self#compute (n-1)) + (self#compute (n-2))
- method say () = pr "I love sushis !\n";
- end
- module MyT = struct
- class c = fibo
- end
- let _ =
- pr "Méthode (M)némo\n";
- let module Memo_fibo = Memo(MyT) in (* <== Error : say cannot be hidden *)
- let f2 = new Memo_fibo.c in
- f2#compute 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement