/collects/scribblings/reference/sets.scrbl

http://github.com/agocke/racket · Racket · 259 lines · 178 code · 81 blank · 0 comment · 9 complexity · 82d2869b71a8a21f687ff920a04c175d MD5 · raw file

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