Advertisement
anthonimes

TP5

Mar 31st, 2020
2,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.28 KB | None | 0 0
  1. (* Exercice 1 *)
  2.  
  3. (* 1 *)
  4. type natural = Zero | S of natural ;;
  5.  
  6. (* 2 *)
  7. let rec addition = fun n1 -> fun n2 ->
  8.   match n1 with
  9.   | Zero -> n2
  10.   | (* ? *)
  11.  
  12. addition (S(Zero)) (S(Zero)) ;;
  13.  
  14. let rec multiplication = fun n1 n2 ->
  15.   match n1 with
  16.   | (* ? *)
  17.  
  18. multiplication (S(S(Zero))) (S Zero);;
  19.  
  20. (* 3 *)
  21. let rec factorial = fun n ->
  22.   match n with
  23.   | Zero -> (* ? *)
  24.   | (* ? *)
  25.  
  26. factorial (S (S Zero)) ;;
  27.  
  28. factorial (S (S (S Zero)))  ;;
  29.  
  30. (* Exercice 2 *)
  31. let rec string_of_natural = fun m ->
  32.   match m with
  33.   | Zero -> "0"
  34.   | (* ? *)  
  35.  
  36. string_of_natural (S (S Zero)) ;;  
  37.  
  38. let rec natural_of_string = fun s ->
  39.   let lg = String.length s in
  40.   if lg = 1 then Zero
  41.   (* ? *) ;;
  42.    
  43. natural_of_string "SS0" ;;
  44.  
  45. let rec natural_of_int = fun n ->
  46.   if n < 0 then failwith "négatif !" else
  47.   match n with
  48.   (* ? *);;
  49.      
  50. natural_of_int 7 ;;
  51.  
  52. (* A FAIRE : int_of_natural *)
  53.  
  54. int_of_natural (S (S (S Zero))) ;;
  55.  
  56. (* Exercice 3 *)
  57. type pile = natural list  ;;
  58.  
  59. let string_of_pile = fun (p:pile) ->
  60.   (* Une fonction aux(iliaire) peut être utile *)
  61.    match p with
  62.    | [] -> (* ? *)
  63.    | (* ? *)
  64.   in aux 1 p "" ;;
  65.  
  66. print_string (string_of_pile []) ;;
  67.  
  68. let s= string_of_pile [natural_of_int 2; natural_of_string "SSSS0"; S(Zero)] ;;
  69.  
  70. print_string s;;
  71.  
  72. (* Exercice 4 *)
  73. type calculator_input =
  74. | Natural of natural
  75. | Addition
  76. | Multiplication
  77. | Factorial
  78. | Clear
  79.  
  80. let calculator = fun p c ->
  81.   match c with
  82.   | Natural n -> n::p
  83.   | Addition -> begin match p with
  84.                   [] -> failwith "operation incorrecte! "
  85.                   | (* ? *)
  86.                   | (* ? *)
  87.                 end
  88.   | Multiplication -> begin (* ?*) end
  89.   | Factorial -> begin (* ? *)  end
  90.   | Clear -> []
  91.  
  92. exception Stop ;;
  93.  
  94. let input_of_string = fun s ->
  95.   if s = "" then raise Stop else
  96.   (* Regarder le premier caractere est suffisant *)  
  97.   let c = s.[0] in
  98.    match c with
  99.    | '0' -> Natural (Zero)(* ? *)
  100.    | (* ? *)
  101.    | _ -> raise Stop ;;
  102.    
  103.    
  104. input_of_string "C" ;;
  105.  
  106. input_of_string "SSS0" ;;
  107.  
  108. input_of_string "+" ;;
  109.  
  110. input_of_string "F" ;;
  111.  
  112. (* Exercice 5 *)
  113. let rec top_level = fun (p:pile) ->
  114.   (* ? *)
  115.  
  116. let main() =
  117.   print_string "Calculatrice de Peano";
  118.   print_newline();
  119.   top_level [] ;;
  120.  
  121. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement