Advertisement
Guest User

Untitled

a guest
Jan 29th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.54 KB | None | 0 0
  1. let pr = Printf.printf
  2.  
  3. (** Question 1 : Méthode bourrine *)
  4.  
  5. class fibo =
  6.   object(self)
  7.     method compute n =
  8.       pr "I want to compute fibo(%d) !\n" n;
  9.       match n with
  10.         0 -> 0
  11.       | 1 -> 1
  12.       | _ ->  (self#compute (n-1)) + (self#compute (n-2))
  13.   end
  14.  
  15.  
  16. let _ =
  17.   pr "Méthode bourrine\n";
  18.   let f = new fibo in
  19.   f#compute 5
  20.  
  21. (** Question 2 : (m)némo le petit poisson qui avait de la mémoire *)
  22.  
  23. module type T = sig
  24.   class c : object
  25.     method compute : int -> int
  26.   end
  27. end
  28.  
  29. module Memo (M:T) : T = struct
  30.   class c =
  31.     object(self)
  32.       inherit M.c as poisson_rouge
  33.       val table = Hashtbl.create 100
  34.       method compute n =
  35.         try
  36.           Hashtbl.find table n
  37.         with Not_found ->
  38.           let r = poisson_rouge#compute n in
  39.           Hashtbl.add table n r ;
  40.           r
  41.     end
  42. end
  43.  
  44. module MyT = struct
  45.   class c = fibo      
  46. end
  47.  
  48. let _ =
  49.   pr "Méthode (M)némo\n";
  50.   let module Memo_fibo = Memo(MyT) in
  51.   let f2 = new Memo_fibo.c in
  52.   f2#compute 5
  53.  
  54.  
  55. (** Question 3 : Vive les sushis ! *)
  56.  
  57. class fibo =
  58.   object(self)
  59.     method compute n =
  60.       pr "I want to compute fibo(%d) !\n" n;
  61.       match n with
  62.         0 -> 0
  63.       | 1 -> 1
  64.       | _ ->  (self#compute (n-1)) + (self#compute (n-2))
  65.     method say () = pr "I love sushis !\n";
  66.   end
  67.  
  68. module MyT = struct
  69.   class c = fibo      
  70. end
  71.  
  72. let _ =
  73.   pr "Méthode (M)némo\n";
  74.   let module Memo_fibo = Memo(MyT) in (* <== Error : say cannot be hidden *)
  75.   let f2 = new Memo_fibo.c in
  76.   f2#compute 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement