Advertisement
karol_dziachan

Dictionary <= (key, value) OCaml

Jan 8th, 2021
3,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.73 KB | None | 0 0
  1. module type DICTIONARY =
  2.     sig
  3.       type 'b key  
  4.       type 'a dict
  5.       exception DuplicatedKey of string
  6.         exception FailureKey of string
  7.       val empty: unit -> 'a dict
  8.       val insert: 'a dict -> 'b key * 'a-> 'a dict
  9.       val delete: 'a dict -> 'b key -> 'a dict
  10.       val update: 'a dict -> 'b key -> 'b key * 'a -> 'a dict
  11.       val search: 'a dict -> 'b key -> 'b key * 'a
  12.     end;;
  13.  
  14.  
  15.  
  16. module Dict  =  
  17.       struct
  18.         type 'b key = 'b  
  19.         type 'a dict = ('a key * 'a) list
  20.         exception DuplicatedKey of string
  21.         exception FailureKey of string
  22.         let empty() = []
  23.         let insert dict (key, value) =
  24.           let rec helper = function
  25.           h :: t -> if fst(h) = key then raise (DuplicatedKey "module Dict: insert duplicate")
  26.            else if fst(h) < key && (t = []  || fst(List.hd t) > key) then  h :: (key, value) :: t
  27.             else h :: helper t
  28.           | [] -> [(key, value)]
  29.         in helper(dict)
  30.  
  31.         let rec drop n xs=
  32.           if n<0 then xs
  33.           else
  34.             match (n, xs) with
  35.               (0, h::t) -> t
  36.              |(_, []) -> xs
  37.              |(n, h::t) -> h :: drop (n-1) t;;
  38.  
  39.         let delete dict key=
  40.           let rec helper (actual, n) =
  41.             match actual with
  42.               h::t -> if fst(h) = key then  drop n dict
  43.                       else helper (t, n+1)
  44.             |  [] -> raise (FailureKey "module Dict: delete fault key")
  45.           in helper (dict, 0)
  46.  
  47.           let search dict key =
  48.             let rec helper = function
  49.               h::t -> if fst(h) = key then h
  50.                       else helper t
  51.               | [] -> raise (FailureKey "module Dict: search fault key")
  52.             in helper dict
  53.  
  54.         let update dict key (newKey, newValue)=
  55.           let rec helper = function
  56.             h::t -> if fst(h) = key then (newKey, newValue) :: t
  57.                     else h :: helper t
  58.             | [] -> raise (FailureKey "module Dict: update fault key")
  59.           in helper dict          
  60.         end;;
  61.      
  62. let dicti = Dict.empty();;
  63. let dicti = Dict.insert dicti (1, "Karol");;
  64. let dicti = Dict.insert dicti (2, "Weronika");;
  65. let dicti = Dict.insert dicti (3, "Fabian");;
  66. let dicti = Dict.insert dicti (4, "Aneta");;
  67. let dicti = Dict.insert dicti (6, "Tomasz");;
  68. let dicti = Dict.insert dicti (5, "Tester");;
  69. let dicti = Dict.insert dicti (9, "Nina");;
  70. let dicti = Dict.insert dicti (10, "Oliwia");;
  71. let dicti = Dict.insert dicti (8, "Malgorzata");;
  72. let dicti = Dict.insert dicti (7, "Agata");;
  73.  
  74. let dicti = Dict.delete dicti 3;;
  75. let dicti = Dict.delete dicti 7;;
  76.  
  77. Dict.search dicti 9;;
  78.  
  79. let dicti = Dict.update dicti 8 (11, "Karolina");;
  80. Dict.search dicti 11;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement