diff --git a/collects/racket/set.rkt b/collects/racket/set.rkt index 2676037d7f..3dc363eb81 100644 --- a/collects/racket/set.rkt +++ b/collects/racket/set.rkt @@ -6,6 +6,7 @@ set-empty? set-count set-member? set-add set-remove set-union set-intersect set-subtract + set-subset? set-map set-for-each (rename-out [*in-set in-set]) for/set for/seteq for/seteqv @@ -163,6 +164,18 @@ (for/fold ([set set]) ([set2 (in-list sets)]) (set-subtract set set2))])) +(define (set-subset? set1 set2) + (unless (set? set1) (raise-type-error 'set-subset? "set" 0 set1 set2)) + (unless (set? set2) (raise-type-error 'set-subset? "set" 1 set1 set2)) + (let ([ht1 (set-ht set1)] + [ht2 (set-ht set2)]) + (unless (and (eq? (hash-eq? ht1) (hash-eq? ht2)) + (eq? (hash-eqv? ht1) (hash-eqv? ht2))) + (raise-mismatch-error 'set-subset? "second set's equivalence predicate is not the same as the first set: " + set2)) + (for/and ([v (in-hash-keys ht2)]) + (hash-ref ht1 v #f)))) + (define (set-map set proc) (unless (set? set) (raise-type-error 'set-map "set" 0 set proc)) (unless (and (procedure? proc) diff --git a/collects/scribblings/reference/sets.scrbl b/collects/scribblings/reference/sets.scrbl index 32bcf72223..a61ff00c9f 100644 --- a/collects/scribblings/reference/sets.scrbl +++ b/collects/scribblings/reference/sets.scrbl @@ -52,11 +52,13 @@ to a later element takes precedence over the later element.} Returns @scheme[#t] if @scheme[set] has no members, @scheme[#f] otherwise.} + @defproc[(set-member? [set set?] [v any/c]) boolean?]{ Returns @scheme[#t] if @scheme[v] is in @scheme[set], @scheme[#f] otherwise.} + @defproc[(set-add [set set?] [v any/c]) set?]{ @margin-note{Like operations on immutable hash tables, ``constant @@ -101,6 +103,15 @@ runs in time proportional to the total size of all given @scheme[set]s except the first one.} +@defproc[(set-subset? [set set?] [set2 set?]) boolean?]{ + +Returns @scheme[#t] if every member of @scheme[set2] is in +@scheme[set], @scheme[#f] otherwise. The @scheme[set] and +@scheme[set2] must use the same equivalence predicate +(@scheme[equal?], @scheme[eq?], or @scheme[eqv?]). This operation +runs in time proportional to the size of @scheme[set2].} + + @defproc[(set-map [set set?] [proc (any/c . -> . any/c)]) (listof any/c)]{