Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. type ordering = LT | EQ | GT;;
  2.  
  3. module type ORDER =
  4. sig
  5. type t
  6. val compare: t -> t -> ordering
  7. end;;
  8.  
  9.  
  10. module StringOrder: ORDER with type t = string =
  11. struct
  12. type t = string
  13. let compare s1 s2 = if s1<s2 then LT else
  14. if s1>s2 then GT else EQ
  15. end;;
  16.  
  17. module IntOrder: ORDER with type t = int =
  18. struct
  19. type t = int
  20. let compare i1 i2 = if i1<i2 then LT else
  21. if i1>i2 then GT else EQ
  22. end;;
  23.  
  24.  
  25. module type DICTIONARY =
  26. sig
  27. type key
  28. (* type of keys *)
  29. type 'a t
  30. (* type of dictionaries *)
  31. exception DuplicatedKey of key
  32. (* error in insert *)
  33. val empty: unit -> 'a t
  34. (* empty dictionary *)
  35. val lookup: 'a t -> key -> 'a option
  36. val insert: 'a t -> key * 'a -> 'a t
  37. val delete: 'a t -> key -> 'a t
  38. val update: 'a t -> key * 'a -> 'a t
  39. end;;
  40.  
  41. module Dictionary (Key : ORDER) : DICTIONARY with type key = Key.t =
  42. struct
  43. type key = Key.t
  44. type 'a t = (key * 'a) list
  45. exception DuplicatedKey of key
  46.  
  47. let empty() = []
  48.  
  49. let rec lookup xs key =
  50. match xs with
  51. (k, v)::t -> (match Key.compare key k with
  52. LT -> None
  53. | EQ -> Some v
  54. | GT -> lookup t key)
  55. | [] -> None
  56.  
  57. let rec insert xs (key, value) =
  58. match xs with
  59. (k, v)::t as l -> (match Key.compare key k with
  60. LT -> (key, value)::l
  61. | EQ -> raise (DuplicatedKey key)
  62. | GT -> (k, v)::(insert t (key, value)))
  63. | [] -> [key, value]
  64.  
  65. let rec delete xs key =
  66. match xs with
  67. (k, v)::t as l -> (match Key.compare key k with
  68. LT -> l
  69. | EQ -> t
  70. | GT -> (k, v)::(delete t key))
  71. | [] -> []
  72.  
  73. let rec update xs (key, value) =
  74. match xs with
  75. (k, v)::t as l -> (match Key.compare key k with
  76. LT -> (key, value)::l
  77. | EQ -> (key, value)::t
  78. | GT -> (k, v)::(update t (key, value))
  79. )
  80. | [] -> [key, value]
  81. end;;
  82.  
  83.  
  84. module StringDict = Dictionary(StringOrder);;
  85.  
  86. module IntDict = Dictionary(IntOrder);;
  87.  
  88. StringDict.lookup
  89. (StringDict.insert
  90. (StringDict.insert
  91. (StringDict.empty())
  92. ("a", 10))
  93. ("b", 20))
  94. "a";;
  95.  
  96. IntDict.lookup
  97. (IntDict.insert
  98. (IntDict.insert
  99. (IntDict.empty())
  100. (1, "jeden"))
  101. (2, "dwa"))
  102. 1;;
  103.  
  104. let ( <| ) d (k,x) = StringDict.update d (k,x);;
  105.  
  106. let dict = StringDict.empty();;
  107.  
  108. let dict = dict <| ("kot", "cat")
  109. <| ("slon", "elephant")
  110. <| ("pies", "dog")
  111. <| ("ptak", "bird");;
  112.  
  113. StringDict.lookup dict "kot";;
  114.  
  115. StringDict.lookup (StringDict.update dict ("kot", "kotek")) "kot" ;;
  116.  
  117. let dict = StringDict.delete dict "ptak";;
  118.  
  119. StringDict.lookup dict "pies";;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement