filter(3scm) Scheme Programmer's Manual filter(3scm)

filter, partition - filter a list

(import (rnrs))                     ;R6RS
(import (rnrs lists))               ;R6RS

(filter proc list)
(partition proc list)

The filter procedure returns a list containing the elements in list for which proc returned a true value.

The partition procedure is exactly like filter, except it also returns a second list containing the elements that were eliminated by proc.

The elements of the returned lists are in the same order as they appear in list. The proc procedure is called in an unspecified order.

Proc should accept one argument and return a single value. Proc should not mutate list. Proc is always called in the same dynamic environment as filter or partition itself.

The filter procedure returns a single list. The partition procedure returns two list.

If this procedure returns twice (e.g. by use of call/cc) then the lists returned earlier are not mutated.

(filter even? '(3 1 4 1 5 9 2 6))
           => (4 2 6)
(partition even? '(3 1 4 1 5 9 2 6))
           => (4 2 6) (3 1 1 5 9) ; two values

These procedures are common in functional code that works with lists.

These procedures are absent from reports other than R6RS. See SRFI-1 for alternatives.

This procedure can raise exceptions with the following condition types:
&assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain. Examples of this are a procedure proc that does not accept and return a single value, or a list which is circular or improper.

map(3scm), remp(3scm)

R6RS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

The procedure proc should avoid side-effects, because the order of calls is unspecified.
2021-03-03