# (lispkit set)

Library `(lispkit set)` provides a generic implementation for sets of objects. Its API design is compatible to the R6RS-style API of library `(lispkit hashtable)`.

A set is a data structure for representing collections of objects. Any object can be used as element, provided a hash function and a suitable equivalence function is available. A hash function is a procedure that maps elements to exact integer objects. It is the programmer’s responsibility to ensure that the hash function is compatible with the equivalence function, which is a procedure that accepts two objects and returns true if they are equivalent and `#f` otherwise. Standard sets for arbitrary objects based on the `eq?`, `eqv?`, and `equal?` predicates are provided.

## Constructors

**set-type-tag** <img src="/files/viHiVHsCY8Wn2TeCgWJj" alt="" data-size="line">

Symbol representing the `set` type. The `type-for` procedure of library `(lispkit type)` returns this symbol for all set objects.

**(make-eq-set)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Create a new empty set using `eq?` as equivalence function.

**(make-eqv-set)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Create a new empty set using `eqv?` as equivalence function.

**(make-equal-set)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Create a new empty set using `equal?` as equivalence function.

**(make-set&#x20;*****hash equiv*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">\
\&#xNAN;**(make-set&#x20;*****hash equiv k*****)**

Create a new empty set using the given hash function *hash* and equivalence function *equiv*. An initial capacity *k* can be provided optionally.

**(eq-set&#x20;*****element ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Create a new set using `eq?` as equivalence function. Initialize it with the values *element ...* .

**(eqv-set&#x20;*****element ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Create a new set using `eqv?` as equivalence function. Initialize it with the values *element ...* .

**(equal-set&#x20;*****element ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Create a new set using `equal?` as equivalence function. Initialize it with the values *element ...* .

## Inspection

**(set-equivalence-function&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the equivalence function used by set *s*.

**(set-hash-function&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the hash function used by set *s*.

**(set-mutable?&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if set *s* is mutable.

## Predicates

**(set?&#x20;*****obj*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if *obj* is a set.

**(set-empty?&#x20;*****obj*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if *obj* is an empty set.

**(set=?&#x20;*****s1 s2*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if set *s1* and set *s2* are using the same equivalence function and contain the same elements.

**(disjoint?&#x20;*****s1 s2*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if set *s1* and set *s2* are disjoint sets.

**(subset?&#x20;*****s1 s2*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if set *s1* is a subset of set *s2*.

**(proper-subset?&#x20;*****s1 s2*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if set *s1* is a proper subset of set *s2*, i.e. *s1* is a subset of *s2* and *s1* is not equivalent to *s2*.

**(set-contains?&#x20;*****s element*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#` if set *s* contains *element*.

**(set-any?&#x20;*****s proc*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns true if there is at least one element in set *s* for which procedure *proc* returns true (i.e. not `#f`).

**(set-every?&#x20;*****s proc*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns true if procedure *proc* returns true (i.e. not `#f`) for all elements of set *s*.

## Procedures

**(set-size&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the number of elements in set *s*.

**(set-elements&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the elements of set *s* as a vector.

**(set-copy s)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">\
\&#xNAN;**(set-copy&#x20;*****s mutable*****)**

Copies set *s* creating an immutable copy if `mutable` is set to `#f` or if `mutable` is not provided.

**(set-for-each&#x20;*****s proc*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Applies procedure *proc* to all elements of set *s* in an undefined order.

**(set-filter&#x20;*****s pred*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set containing the elements of set *s* for which the procedure *pred* returns true.

**(set-union&#x20;*****s s1 ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set containing the union of *s* with *s1 ...*.

**(set-intersection&#x20;*****s s1 ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set containing the intersection of *s* with *s1 ...*.

**(set-difference&#x20;*****s s1 ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set containing the difference of `s` and the sets in *s1 ...* .

**(set->list&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the elements of set *s* as a list.

**(list->eq-set&#x20;*****elements*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set using the equivalence function `eq?` from the values in list *elements*.

**(list->eqv-set&#x20;*****elements*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set using the equivalence function `eqv?` from the values in list *elements*.

**(list->equal-set&#x20;*****elements*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Creates a new set using the equivalence function `equal?` from the values in list *elements*.

## Mutators

**(set-adjoin!&#x20;*****s element ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Adds *element ...* to the set *s*.

**(set-delete!&#x20;*****s element ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Deletes *element ...* from the set *s*.

**(set-clear!&#x20;*****s*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">\
\&#xNAN;**(set-clear!&#x20;*****s k*****)**

Clears set *s* and reserves a capacity of *k* elements if *k* is provided.

**(list->set!&#x20;*****s elements*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Adds the values of list *elements* to set *s*.

**(set-filter!&#x20;*****s pred*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Removes all elements from set *s* for which procedure *pred* returns `#f`.

**(set-union!&#x20;*****s s1 ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Stores the union of set *s* and sets *s1 ...* in *s*.

**(set-intersection!&#x20;*****s s1 ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Stores the intersection of set *s* and the sets *s1 ...* in *s*.

**(set-difference!&#x20;*****s s1 ...*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Stores the difference of set *s* and the sets *s1 ...* in *s*.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.lisppad.app/libraries/lispkit/lispkit-set.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
