Advertisement
Keodedad

Untitled

Apr 5th, 2019
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.25 KB | None | 0 0
  1. module type DICTIONNAIRE =
  2. sig
  3.   type ('a, 'b) t
  4.   val creer : unit -> ('a,'b) t
  5.   val ajouter :
  6.     'a -> 'b -> ('a,'b) t -> ('a,'b) t
  7.   val acceder : 'a -> ('a,'b) t -> 'b
  8.   val appartient : 'a -> ('a,'b) t -> bool
  9.   val supprimer :
  10.     'a -> ('a,'b) t -> ('a,'b) t
  11. end
  12.  
  13. module Dictionnaire : DICTIONNAIRE =
  14.   struct
  15.  
  16.     type ('a, 'b) t =
  17.         Vide
  18.       | Element of
  19.           'a * 'b * ('a,'b) t
  20.  
  21.     exception Pas_presente
  22.      
  23.     let creer = fun () -> Vide
  24.    
  25.     let rec ajouter =
  26.       fun x y d ->
  27.       match d with
  28.         Vide -> Element (x,y,d)
  29.       | Element (x',y',d') when x = x' ->
  30.         Element (x', y, d')
  31.       | Element (x,y,d') ->
  32.         Element(x,y, ajouter x y d')
  33.  
  34.     let rec acceder =
  35.       fun x d ->
  36.       match d with
  37.         Vide -> raise Pas_presente
  38.       | Element (x',y,d') when x = x' -> y
  39.       | Element (_ ,_ , d') -> acceder x d'
  40.  
  41.     let rec appartient =
  42.       fun x d ->
  43.       try
  44.         let _ = acceder x d in true
  45.       with Pas_presente -> false
  46.  
  47.     let rec supprimer =
  48.       fun x d ->
  49.       match d with
  50.         Vide -> raise Pas_presente
  51.       | Element (x',y,d') when x = x' -> d'
  52.       | Element (x' ,y' , d') ->
  53.         Element (x',y', supprimer x d')
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement