Lists as sets
.............
`eqv?' is used to test for membership by procedures which treat lists
as sets.
- Function: adjoin e l
`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)
(adjoin 'foo '(bar baz bang))
=> (foo bar baz bang)
- Function: union l1 l2
`union' returns the combination of L1 and 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))
=> (8 7 6 5 1 2 3 4)
(union '(1 2 3 4) '(3 4 5 6))
=> (6 5 1 2 3 4)
- Function: intersection l1 l2
`intersection' returns 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 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: member-if pred lst
`member-if' returns LST if `(PRED ELEMENT)' is `#t' for any
ELEMENT in LST. Returns `#f' if PRED does not apply to any
ELEMENT in LST.
Example:
(member-if vector? '(1 2 3 4))
=> #f
(member-if number? '(1 2 3 4))
=> (1 2 3 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 > '(2 3) '(1 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)