Filtering Lists - MIT/GNU Scheme 7.7.90+
Description: MIT/GNU Scheme 7.7.90+

Next: Searching Lists, Previous: Cutting and Pasting Lists, Up: Lists

### 7.5 Filtering Lists

— procedure: keep-matching-items list predicate
— procedure: delete-matching-items list predicate

> These procedures return a newly allocated copy of list containing only the elements for which predicate is (respectively) true or false. Predicate must be a procedure of one argument.

>               (keep-matching-items '(1 2 3 4 5) odd?) => (1 3 5)
>               (delete-matching-items '(1 2 3 4 5) odd?) => (2 4)

> For compatibility, the procedure `list-transform-positive` is an alias for `keep-matching-items`, and `list-transform-negative` is an alias for `delete-matching-items`.

— procedure: keep-matching-items! list predicate
— procedure: delete-matching-items! list predicate

> These procedures are exactly like `keep-matching-items` and `delete-matching-items`, respectively, except that they destructively modify the list argument rather than allocating a new result.

— procedure: delq element list
— procedure: delv element list
— procedure: delete element list

> Returns a newly allocated copy of list with all entries equal to element removed. `delq` uses `eq?` to compare element with the entries in list, `delv` uses `eqv?`, and `delete` uses `equal?`.

— procedure: delq! element list
— procedure: delv! element list
— procedure: delete! element list

> Returns a list consisting of the top-level elements of list with all entries equal to element removed. These procedures are like `delq`, `delv`, and `delete` except that they destructively modify list. `delq!` uses `eq?` to compare element with the entries in list, `delv!` uses `eqv?`, and `delete!` uses `equal?`. Because the result may not be `eq?` to list, it is desirable to do something like `(set! x (delete! x))`.

>               (define x '(a b c b))
>               (delete 'b x)                           =>  (a c)
>               x                                       =>  (a b c b)

>               (define x '(a b c b))
>               (delete! 'b x)                          =>  (a c)
>               x                                       =>  (a c)
>               ;; Returns correct result:
>               (delete! 'a x)                          =>  (c)

>               ;; Didn't modify what x points to:
>               x                                       =>  (a c)

— procedure: delete-member-procedure deletor predicate

> Returns a deletion procedure similar to `delv` or `delete!`. Deletor should be one of the procedures `list-deletor` or `list-deletor!`. Predicate must be an equivalence predicate. The returned procedure accepts exactly two arguments: first, an object to be deleted, and second, a list of objects from which it is to be deleted. If deletor is `list-deletor`, the procedure returns a newly allocated copy of the given list in which all entries equal to the given object have been removed. If deletor is `list-deletor!`, the procedure returns a list consisting of the top-level elements of the given list with all entries equal to the given object removed; the given list is destructively modified to produce the result. In either case predicate is used to compare the given object to the elements of the given list.

> Here are some examples that demonstrate how `delete-member-procedure` could have been used to implement `delv` and `delete!`:

>               (define delv
>                 (delete-member-procedure list-deletor eqv?))
>               (define delete!
>                 (delete-member-procedure list-deletor! equal?))

— procedure: list-deletor predicate
— procedure: list-deletor! predicate

> These procedures each return a procedure that deletes elements from lists. Predicate must be a procedure of one argument. The returned procedure accepts exactly one argument, which must be a proper list, and applies predicate to each of the elements of the argument, deleting those for which it is true.

> The procedure returned by `list-deletor` deletes elements non-destructively, by returning a newly allocated copy of the argument with the appropriate elements removed. The procedure returned by `list-deletor!` performs a destructive deletion.