Next: , Previous: List construction, Up: Common List Functions

##### 7.2.1.2 Lists as sets

`eqv?` is used to test for membership by procedures which treat lists as sets.

`adjoin` returns the adjoint of the element e and the list l. That is, if e is in l, `adjoin` returns l, otherwise, it returns `(cons `e l`)`.

Example:

```          (adjoin 'baz '(bar baz bang))
⇒ (bar baz bang)
⇒ (foo bar baz bang)
```
— Function: union l1 l2

`union` returns a list of all elements that are in l1 or l2. Duplicates between l1 and l2 are culled. Duplicates within l1 or within l2 may or may not be removed.

Example:

```          (union '(1 2 3 4) '(5 6 7 8))
⇒ (1 2 3 4 5 6 7 8)
(union '(0 1 2 3 4) '(3 4 5 6))
⇒ (5 6 0 1 2 3 4)
```
— Function: intersection l1 l2

`intersection` returns a list of all elements that are in both l1 and l2.

Example:

```          (intersection '(1 2 3 4) '(3 4 5 6))
⇒ (3 4)
(intersection '(1 2 3 4) '(5 6 7 8))
⇒ ()
```
— Function: set-difference l1 l2

`set-difference` returns a list of all elements that are in l1 but not in l2.

Example:

```          (set-difference '(1 2 3 4) '(3 4 5 6))
⇒ (1 2)
(set-difference '(1 2 3 4) '(1 2 3 4 5 6))
⇒ ()
```
— Function: subset? list1 list2

Returns `#t` if every element of list1 is `eqv?` an element of list2; otherwise returns `#f`.

Example:

```          (subset? '(1 2 3 4) '(3 4 5 6))
⇒ #f
(subset? '(1 2 3 4) '(6 5 4 3 2 1 0))
⇒ #t
```
— Function: member-if pred lst

`member-if` returns the list headed by the first element of lst to satisfy `(`pred element`)`. `Member-if` returns `#f` if pred returns `#f` for every element in lst.

Example:

```          (member-if vector? '(a 2 b 4))
⇒ #f
(member-if number? '(a 2 b 4))
⇒ (2 b 4)
```
— Function: some pred lst1 lst2 ...

pred is a boolean function of as many arguments as there are list arguments to `some` i.e., lst plus any optional arguments. pred is applied to successive elements of the list arguments in order. `some` returns `#t` as soon as one of these applications returns `#t`, and is `#f` if none returns `#t`. All the lists should have the same length.

Example:

```          (some odd? '(1 2 3 4))
⇒ #t

(some odd? '(2 4 6 8))
⇒ #f

(some > '(1 3) '(2 4))
⇒ #f
```
— Function: every pred lst1 lst2 ...

`every` is analogous to `some` except it returns `#t` if every application of pred is `#t` and `#f` otherwise.

Example:

```          (every even? '(1 2 3 4))
⇒ #f

(every even? '(2 4 6 8))
⇒ #t

(every > '(2 3) '(1 4))
⇒ #f
```
— Function: notany pred lst1 ...

`notany` is analogous to `some` but returns `#t` if no application of pred returns `#t` or `#f` as soon as any one does.

— Function: notevery pred lst1 ...

`notevery` is analogous to `some` but returns `#t` as soon as an application of pred returns `#f`, and `#f` otherwise.

Example:

```          (notevery even? '(1 2 3 4))
⇒ #t

(notevery even? '(2 4 6 8))
⇒ #f
```
— Function: list-of?? predicate

Returns a predicate which returns true if its argument is a list every element of which satisfies predicate.

— Function: list-of?? predicate low-bound high-bound

low-bound and high-bound are non-negative integers. `list-of??` returns a predicate which returns true if its argument is a list of length between low-bound and high-bound (inclusive); every element of which satisfies predicate.

— Function: list-of?? predicate bound

bound is an integer. If bound is negative, `list-of??` returns a predicate which returns true if its argument is a list of length greater than `(- `bound`)`; every element of which satisfies predicate. Otherwise, `list-of??` returns a predicate which returns true if its argument is a list of length less than or equal to bound; every element of which satisfies predicate.

— Function: find-if pred lst

`find-if` searches for the first element in lst such that `(`pred element`)` returns `#t`. If it finds any such element in lst, element is returned. Otherwise, `#f` is returned.

Example:

```          (find-if number? '(foo 1 bar 2))
⇒ 1

(find-if number? '(foo bar baz bang))
⇒ #f

(find-if symbol? '(1 2 foo bar))
⇒ foo
```
— Function: remove elt lst

`remove` removes all occurrences of elt from lst using `eqv?` to test for equality and returns everything that's left. N.B.: other implementations (Chez, Scheme->C and T, at least) use `equal?` as the equality test.

Example:

```          (remove 1 '(1 2 1 3 1 4 1 5))
⇒ (2 3 4 5)

(remove 'foo '(bar baz bang))
⇒ (bar baz bang)
```
— Function: remove-if pred lst

`remove-if` removes all elements from lst where `(`pred element`)` is `#t` and returns everything that's left.

Example:

```          (remove-if number? '(1 2 3 4))
⇒ ()

(remove-if even? '(1 2 3 4 5 6 7 8))
⇒ (1 3 5 7)
```
— Function: remove-if-not pred lst

`remove-if-not` removes all elements from lst for which `(`pred element`)` is `#f` and returns everything that's left.

Example:

```          (remove-if-not number? '(foo bar baz))
⇒ ()
(remove-if-not odd? '(1 2 3 4 5 6 7 8))
⇒ (1 3 5 7)
```
— Function: has-duplicates? lst

returns `#t` if 2 members of lst are `equal?`, `#f` otherwise.

Example:

```          (has-duplicates? '(1 2 3 4))
⇒ #f

(has-duplicates? '(2 4 3 4))
⇒ #t
```

The procedure `remove-duplicates` uses `member` (rather than `memv`).

— Function: remove-duplicates lst

returns a copy of lst with its duplicate members removed. Elements are considered duplicate if they are `equal?`.

Example:

```          (remove-duplicates '(1 2 3 4))
⇒ (1 2 3 4)

(remove-duplicates '(2 4 3 4))
⇒ (2 4 3)
```