Next: , Previous: , Up: Lists   [Contents][Index]

7.5 Filtering Lists

SRFI 1 procedure: filter predicate list

Returns a newly allocated copy of list containing only the elements satisfying predicate. Predicate must be a procedure of one argument.

```(filter odd? '(1 2 3 4 5)) ⇒ (1 3 5)
```
SRFI 1 procedure: remove predicate list

Like `filter`, except that the returned list contains only those elements not satisfying predicate.

```(remove odd? '(1 2 3 4 5)) ⇒ (2 4)
```
SRFI 1 procedure: partition predicate list

Partitions the elements of list with predicate, and returns two values: the list of in-elements and the list of out-elements. The list is not disordered—elements occur in the result lists in the same order as they occur in the argument list. The dynamic order in which the various applications of `predicate` are made is not specified. One of the returned lists may share a common tail with the argument list.

```(partition symbol? '(one 2 3 four five 6)) =>
(one four five)
(2 3 6)
```
SRFI 1 procedure: filter! predicate list
SRFI 1 procedure: remove! predicate list
SRFI 1 procedure: partition! predicate list

Linear-update variants of `filter`, `remove` and `partition`. These procedures are allowed, but not required, to alter the cons cells in the argument list to construct the result lists.

SRFI 1 procedure: delete x list [compare]
SRFI 1 procedure: delete! x list [compare]

`delete` uses the comparison procedure compare, which defaults to `equal?`, to find all elements of list that are equal to x, and deletes them from list. The dynamic order in which the various applications of compare are made is not specified.

The list is not disordered—elements that appear in the result list occur in the same order as they occur in the argument list. The result may share a common tail with the argument list.

Note that fully general element deletion can be performed with the `remove` and `remove!` procedures, e.g.:

```;; Delete all the even elements from LIS:
(remove even? lis)
```

The comparison procedure is used in this way: ```(compare x ei)```. That is, x is always the first argument, and a list element is always the second argument. The comparison procedure will be used to compare each element of list exactly once; the order in which it is applied to the various ei is not specified. Thus, one can reliably remove all the numbers greater than five from a list with `(delete 5 list <)`.

`delete!` is the linear-update variant of `delete`. It is allowed, but not required, to alter the cons cells in its argument list to construct the result.

procedure: delq x list
procedure: delq! x list
procedure: delv x list
procedure: delv! x list

Equivalent to `(delete x list eq?)`, ```(delete! x list eq?)```, `(delete x list eqv?)`, and `(delete! x list eqv?)`, respectively.

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.

Next: Searching Lists, Previous: Cutting and Pasting Lists, Up: Lists   [Contents][Index]