A multiset is like a set, with the difference that a value can appear more than once.
- Implement a module
MultiSet
that implements the signatureMultiSet_S
. - Define a function
letters: string -> char MultiSet.t
(whereMultiSet
is the module defined in the previous question). This function produces a multiset in which all characters are associated to the times they appear in the input string. - Define a function
anagram: string -> string -> bool
that uses the previous function to tell if two words have the same multiset of characters.
module type MultiSet_S = sig
(* A multi-set of type ['a t] is a collection of values of
type ['a] that may occur several times. *)
type 'a t
(* [occurrences s x] return the number of time [x] occurs
in [s]. *)
val occurrences : 'a t -> 'a -> int
(* The empty set has no element. There is only one unique
representation of the empty set. *)
val empty : 'a t
(* [insert s x] returns a new multi-set that contains all
elements of [s] and a new occurrence of [x]. Typically,
[occurrences s x = occurrences (insert s x) x + 1]. *)
val insert : 'a t -> 'a -> 'a t
(* [remove s x] returns a new multi-set that contains all elements
of [s] minus an occurrence of [x] (if [x] actually occurs in
[s]). Typically, [occurrences s x = occurrences (remove s x) x -
1] if [occurrences s x > 0]. *)
val remove : 'a t -> 'a -> 'a t
end
module MultiSet = struct
(* Replace this comment with your implementation. *)
end ;;
let letters word =
"Replace this string with your implementation." ;;
let anagram word1 word2 =
"Replace this string with your implementation." ;;