/collects/scribblings/reference/sets.scrbl

http://github.com/gmarceau/PLT · Racket · 258 lines · 177 code · 81 blank · 0 comment · 9 complexity · 921a56b5edec7b24a782ccf65127c239 MD5 · raw file

  1. #lang scribble/doc
  2. @(require "mz.rkt" (for-label racket/set))
  3. @title[#:tag "sets"]{Sets}
  4. @(define set-eval (make-base-eval))
  5. @(interaction-eval #:eval set-eval (require racket/set))
  6. A @deftech{set} represents a set of distinct elements. For a given
  7. set, elements are equivalent via @racket[equal?], @racket[eqv?], or
  8. @racket[eq?]. Two sets are @racket[equal?] when they use the same
  9. element-comparison procedure (@racket[equal?], @racket[eqv?], or
  10. @racket[eq?]) and have equivalent elements.
  11. A set can be used as a single-valued sequence (see
  12. @secref["sequences"]). The elements of the set serve as elements
  13. of the sequence. See also @racket[in-set].
  14. Operations on sets that contain elements that are mutated are
  15. unpredictable in much the same way that @tech{hash table} operations are
  16. unpredictable when keys are mutated.
  17. @note-lib[racket/set]
  18. @deftogether[(
  19. @defproc[(set [v any/c] ...) set?]
  20. @defproc[(seteqv [v any/c] ...) set?]
  21. @defproc[(seteq [v any/c] ...) set?]
  22. )]{
  23. Creates a set that uses @racket[equal?], @racket[eq?], or
  24. @racket[eqv?], respectively, to compare elements. The given
  25. @racket[v]s are added to the set. The elements are added in the order
  26. that they appear as @racket[v]s, so in the first two cases, an earlier
  27. element that is @racket[equal?] or @racket[eqv?] but not @racket[eq?]
  28. to a later element takes precedence over the later element.}
  29. @defproc[(set-empty? [st set?]) boolean?]{
  30. Returns @racket[#t] if @racket[st] has no members, @racket[#f]
  31. otherwise.}
  32. @defproc[(set-count [st set?]) exact-nonnegative-integer?]{
  33. Returns the number of elements in @racket[st].}
  34. @defproc[(set-member? [st set?] [v any/c]) boolean?]{
  35. Returns @racket[#t] if @racket[v] is in @racket[st], @racket[#f]
  36. otherwise.}
  37. @defproc[(set-add [st set?] [v any/c]) set?]{
  38. @margin-note{Like operations on immutable hash tables, ``constant
  39. time'' set operations actually require @math{O(log N)} time for a set
  40. of size @math{N}.}
  41. Produces a set that includes @racket[v] plus all elements of
  42. @racket[st]. This operation runs in constant time.}
  43. @defproc[(set-remove [st set?] [v any/c]) set?]{
  44. Produces a set that includes all elements of @racket[st] except
  45. @racket[v]. This operation runs in constant time.}
  46. @defproc[(set-union [st set?] ...+) set?]{
  47. Produces a set that includes all elements of all given @racket[st]s,
  48. which must all use the same equivalence predicate (@racket[equal?],
  49. @racket[eq?], or @racket[eqv?]). This operation runs in time
  50. proportional to the total size of all given @racket[st]s except for
  51. the largest.
  52. At least one set must be provided to @racket[set-union] even though
  53. mathematically @racket[set-union] could accept zero arguments. Since
  54. there are multiple types of sets (@racket[eq?], @racket[eqv?], and
  55. @racket[equal?]) there is no obvious choice for a default empty set
  56. to be returned. If there is a case where @racket[set-union] may be
  57. applied to zero arguments, instead pass an empty set of the type
  58. you desire.
  59. @examples[#:eval set-eval
  60. (set-union (set))
  61. (set-union (seteq))
  62. (set-union (set 1) (set 2))
  63. (set-union (set 1) (seteq 2)) (code:comment "Sets of different types cannot be unioned")
  64. ]}
  65. @defproc[(set-intersect [st set?] ...+) set?]{
  66. Produces a set that includes only the elements in all of the given
  67. @racket[st]s, which must all use the same equivalence predicate
  68. (@racket[equal?], @racket[eq?], or @racket[eqv?]). This operation
  69. runs in time proportional to the total size of all given
  70. @racket[st]s except for the largest.}
  71. @defproc[(set-subtract [st set?] ...+) set?]{
  72. Produces a set that includes all elements the first @racket[st]s that
  73. are not present in any of the other given @racket[sts]s. All of the
  74. given @racket[st]s must use the same equivalence predicate
  75. (@racket[equal?], @racket[eq?], or @racket[eqv?]). This operation
  76. runs in time proportional to the total size of all given
  77. @racket[st]s except the first one.}
  78. @defproc[(set-symmetric-difference [st set?] ...+) set?]{
  79. Produces a set containing only those elements found in each
  80. @racket[st] an odd number of times. All of the given @racket[st]s must
  81. use the same equivalence predicate (@racket[equal?], @racket[eq?], or
  82. @racket[eqv?]). This operation runs in time proportional to the total
  83. size of all given @racket[st]s except the first one.
  84. @examples[#:eval set-eval
  85. (set-symmetric-difference (set 1) (set 1 2) (set 1 2 3))
  86. ]}
  87. @defproc[(set=? [st set?] [st2 set?]) boolean?]{
  88. Returns @racket[#t] if @racket[st] and @racket[st2] contain the same
  89. members, @racket[#f] otherwise. The @racket[st] and @racket[st2] must
  90. use the same equivalence predicate (@racket[equal?], @racket[eq?], or
  91. @racket[eqv?]). This operation runs in time proportional to the size
  92. of @racket[st].
  93. Equivalent to @racket[(equal? st st2)].
  94. @examples[#:eval set-eval
  95. (set=? (set 1) (set 1 2 3))
  96. (set=? (set 1 2 3) (set 1))
  97. (set=? (set 1 2 3) (set 1 2 3))
  98. ]}
  99. @defproc[(subset? [st set?] [st2 set?]) boolean?]{
  100. Returns @racket[#t] if every member of @racket[st] is in
  101. @racket[st2], @racket[#f] otherwise. The @racket[st] and
  102. @racket[st2] must use the same equivalence predicate
  103. (@racket[equal?], @racket[eq?], or @racket[eqv?]). This operation
  104. runs in time proportional to the size of @racket[st].
  105. @examples[#:eval set-eval
  106. (subset? (set 1) (set 1 2 3))
  107. (subset? (set 1 2 3) (set 1))
  108. (subset? (set 1 2 3) (set 1 2 3))
  109. ]}
  110. @defproc[(proper-subset? [st set?] [st2 set?]) boolean?]{
  111. Returns @racket[#t] if every member of @racket[st] is in @racket[st2]
  112. and there is some member of @racket[st2] that is not a member of
  113. @racket[st], @racket[#f] otherwise. The @racket[st] and @racket[st2]
  114. must use the same equivalence predicate (@racket[equal?],
  115. @racket[eq?], or @racket[eqv?]). This operation runs in time
  116. proportional to the size of @racket[st].
  117. @examples[#:eval set-eval
  118. (proper-subset? (set 1) (set 1 2 3))
  119. (proper-subset? (set 1 2 3) (set 1))
  120. (proper-subset? (set 1 2 3) (set 1 2 3))
  121. ]}
  122. @defproc[(set-map [st set?]
  123. [proc (any/c . -> . any/c)])
  124. (listof any/c)]{
  125. Applies the procedure @racket[proc] to each element in
  126. @racket[st] in an unspecified order, accumulating the results
  127. into a list.}
  128. @defproc[(set-for-each [st set?]
  129. [proc (any/c . -> . any)])
  130. void?]{
  131. Applies @racket[proc] to each element in @racket[st] (for the
  132. side-effects of @racket[proc]) in an unspecified order.}
  133. @defproc[(set? [v any/c]) boolean?]{
  134. Returns @racket[#t] if @racket[v] is a @tech{set}, @racket[#f]
  135. otherwise.}
  136. @defproc[(set-equal? [st set?]) boolean?]{
  137. Returns @racket[#t] if @racket[st] compares elements with @racket[equal?],
  138. @racket[#f] if it compares with @racket[eqv?] or @racket[eq?].}
  139. @defproc[(set-eqv? [st set?]) boolean?]{
  140. Returns @racket[#t] if @racket[st] compares elements with @racket[eqv?],
  141. @racket[#f] if it compares with @racket[equal?] or @racket[eq?].}
  142. @defproc[(set-eq? [st set?]) boolean?]{
  143. Returns @racket[#t] if @racket[st] compares elements with @racket[eq?],
  144. @racket[#f] if it compares with @racket[equal?] or @racket[eqv?].}
  145. @defproc[(set/c [contract contract?] [#:cmp cmp (or/c 'dont-care 'equal 'eqv 'eq) 'dont-care]) contract?]{
  146. Constructs a contract that recognizes sets whose elements match @racket[contract].
  147. If @racket[cmp] is @racket['dont-care], then the equality notion of the set is not considered
  148. when checking the contract. Otherwise, the contract accepts only sets with the corresponding
  149. notion of equality.
  150. }
  151. @defproc[(in-set [st set?]) sequence?]{
  152. Explicitly converts a set to a sequence for use with @racket[for] and
  153. other forms.}
  154. @deftogether[(
  155. @defform[(for/set (for-clause ...) body ...+)]
  156. @defform[(for/seteq (for-clause ...) body ...+)]
  157. @defform[(for/seteqv (for-clause ...) body ...+)]
  158. @defform[(for*/set (for-clause ...) body ...+)]
  159. @defform[(for*/seteq (for-clause ...) body ...+)]
  160. @defform[(for*/seteqv (for-clause ...) body ...+)]
  161. )]{
  162. Analogous to @racket[for/list] and @racket[for*/list], but to
  163. construct a set instead of a list.}
  164. @deftogether[(
  165. @defproc[(list->set [lst list?]) set?]
  166. @defproc[(list->seteq [lst list?]) set?]
  167. @defproc[(list->seteqv [lst list?]) set?]
  168. )]{
  169. Produces the appropriate type of set containing the elements of the
  170. given list. Equivalent to @racket[(apply set lst)], @racket[(apply
  171. seteq lst)], and @racket[(apply seteqv lst)], respectively.
  172. }
  173. @defproc[(set->list [st set?]) list?]{
  174. Produces a list containing the elements of @racket[st].}
  175. @close-eval[set-eval]
  176. @(define i #'set)
  177. @(define i2 #'set-union)