Advertisement
anthonimes

TP4

Mar 18th, 2020
1,745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.58 KB | None | 0 0
  1. (* Exercice 2.1 *)
  2. type valeur_logique = Vrai | Faux | Indefini ;;
  3.  
  4. type 'a formule_avec_var = Valeur of valeur_logique
  5.                | Variable of 'a
  6.                | Et of 'a formule_avec_var * 'a formule_avec_var
  7.                | Non of 'a formule_avec_var ;;
  8.  
  9.  
  10. let f = Et( Variable "x", Et(Variable "y", Valeur Vrai) ) ;;
  11.  
  12. (* Exercice 2.2 *)
  13. let rec est_close = fun f ->
  14.   match f with
  15.   | Valeur -> true
  16.   | (* ? *)
  17. ;;
  18.  
  19. est_close f;;
  20.  
  21. est_close (Et( Valeur Faux, Et(Valeur Faux, Valeur Vrai) )) ;;
  22.  
  23. (* Exercice 2.3 *)
  24. (* Remplace toutes les occurences de x par g dans f *)
  25. let rec substitution = fun f -> fun x ->
  26.                                fun g ->
  27.                                match f with
  28.                                | Variable y -> if x=y then g else f
  29.                                | (* ? *)
  30. ;;
  31.  
  32. (* Exercice 2.4 *)
  33. (* conv permet de spécifier le convertisseur à utiliser
  34.    en fonction du type de la variable *)
  35. let to_string = fun f -> fun conv ->
  36.                          let rec aux = fun f ->
  37.                            match f with
  38.                            | Variable x -> conv x
  39.                            | Valeur Vrai -> "vrai"
  40.                            | (* ? *)
  41. ;;
  42.  
  43. to_string (Et( Variable "x", Et(Variable "y", Valeur Vrai) )) (fun s -> s) ;;
  44.  
  45. to_string (Et( Variable 1, Et(Variable 2, Valeur Vrai) )) string_of_int ;;
  46.  
  47. let g = Et( Variable 1 , Et(Variable 2, Valeur Vrai) );;
  48.  
  49. to_string g string_of_int ;;
  50.  
  51. (* Exercice 3 *)
  52. type listIS = Vide | I of int * listIS | S of string * listIS ;;
  53.  
  54. let l = I(5, I(3, S("3", I(0, S("", Vide))))) ;;
  55.  
  56. (* Exercice 3.1 *)
  57. let afficheIS = function l ->
  58.   let rec aux = fun l res ->
  59.     match l with
  60.       | Vide -> res
  61.       | (* ? *)
  62.   in print_string (aux l ""); print_newline() ;;
  63.  
  64. afficheIS l ;;
  65.  
  66. (* Exercice 3.2 *)
  67. (* f et g sont les fonctions de conversion des éléments de la listeIS *)
  68. let mapIS_liste = fun f -> fun g -> fun l ->
  69.   let rec aux = fun l res ->
  70.     match l with
  71.       | Vide -> res
  72.       | (* ? *)
  73.   in aux l [];;
  74.  
  75. mapIS_liste (string_of_int) (fun s -> s) l ;;
  76.  
  77. mapIS_liste (fun x -> x=0) (fun x -> x="") l;;
  78.  
  79. (* Exercice 3.3 *)
  80. (* l est une liste de chaînes de caractères *)
  81. let rec liste_to_IS l =
  82.   match l with
  83.   | [] -> Vide
  84.   | h::t -> try (* ? *)
  85.             with
  86.               _ ->  (* ? *);;
  87.  
  88.  
  89. liste_to_IS ["3"; "4"; "toto"];;
  90.  
  91. liste_to_IS ["3"; "4" ];;
  92.  
  93. (* Exercice 3.4 *)
  94. (* ? *)
  95.  
  96. (* Exercice 4 *)
  97. (* à créer sous forme de module ! *)
  98. type ('a, 'b) dictionnaire =
  99.    Vide | Element of 'a * 'b * ('a, 'b) dictionnaire ;;
  100.  
  101.  
  102. (* val creer : unit -> ('a, 'b) dictionnaire = <fun> *)
  103.  
  104. let creer() = Vide;;
  105.                
  106. exception Deja_present;;
  107. (* val ajouter : 'a -> 'b -> ('a, 'b) dictionnaire -> ('a, 'b) dictionnaire = <fun> *)
  108. let rec ajouter = fun c -> fun v ->
  109.                            fun dico ->
  110.                            match dico with
  111.                            | Vide -> Element(c,v,dico)
  112.                            | (* ? *)
  113. ;;                    
  114.  
  115. (* val acceder : 'a -> ('a, 'b) dictionnaire -> 'b = <fun> *)
  116. exception Pas_presente ;;
  117. let rec acceder = fun c -> fun dico ->
  118.                            match dico with
  119.                            | Vide -> raise Pas_presente
  120.                            | (* ? *)
  121.  
  122. (* val appartient : 'a -> ('a, 'b) dictionnaire -> bool = <fun> *)
  123. let rec appartient = fun v ->
  124.   fun dico ->
  125.   match dico with
  126.   | Vide -> false
  127.   | (* ? *)
  128. ;;
  129.  
  130. (* val supprimer : 'a -> ('a, 'b) dictionnaire -> ('a, 'b) dictionnaire = <fun> *)
  131. (* ? *)
  132. let supprimer = fun c dico ->
  133.   (* ? *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement