for-all, exists - apply a predicate to lists of objects
LIBRARY
(import (rnrs)) ;R6RS
(import (rnrs lists)) ;R6RS
SYNOPSIS
(for-all proc list ...)
(exists proc list ...)
DESCRIPTION
These procedures apply
proc
to the elements of the lists. They start with the first
elements of each list, then the second elements, etc. The
for-all
procedure stops when
proc
returns
#f,
and the
exists
procedure stops when
proc
returns a true value.
If iteration
stops because
proc
returns
#f
(for
for-all)
or
a true value
(for
exists)
then that value is returned. Otherwise the return value is as
described in the next two points:
- Empty lists
-
If the lists are all empty then
for-all
returns
#t
and
exists
returns
#f.
- Tail calls
-
If these procedures reach the last elements of the lists then they
perform a tail call of
proc
on the last element of each list.
- Dynamic environment
-
Proc
is always called in the same dynamic environment as
for-all
or
exists
itself. This generally means there is no internal state in the
procedure that would get messed up if there are multiple returns from
proc.
- Matching lengths and arguments
-
The lists should all have the same length, and
proc
should accept as many arguments as there are lists, and return a
single value.
Proc
should not mutate the list arguments.
The implementation must check that the lists are chains of pairs only
to the extent necessary to determine the return value.
RETURN VALUES
These procedures return a single value, which is either a true value
returned by
proc,
or
#f.
A special case is when
proc
is tail called on the last elements of the lists, in which case whatever
proc
returns will returned. Depending on the implementation, it may be
possible for multiple values to be returned, but this should not be
relied upon.
EXAMPLES
(for-all even? '(3 1 4 1 5 9))
=> #f
(for-all even? '(2 4 14))
=> #t
(for-all even? '(2 4 14 . 9))
=> &assertion exception
(for-all (lambda (n) (and (even? n) n))
'(2 4 14))
=> 14
(for-all < '(1 2 3) '(2 3 4))
=> #t
(for-all < '(1 2 4) '(2 3 4))
=> #f
(exists even? '(3 1 4 1 5 9))
=> #t
(exists even? '(3 1 1 5 9))
=> #f
(exists even? '(3 1 1 5 9 . 2))
=> &assertion exception
(exists (lambda (n) (and (even? n) n)) '(2 1 4 14))
=> 2
(exists < '(1 2 4) '(2 3 4))
=> #t
(exists > '(1 2 3) '(2 3 4))
=> #f
(exists values '(#f 42 #f))
=> 42
APPLICATION USAGE
These procedures are used in code that needs to check if all or any
elements in list satisfies some condition. An example would be a
variadic procedure that checks if all its arguments are of a certain
type.
ERRORS
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.
In particular, if the lists are not empty then this will be raised if
proc
is not a procedure that takes the right number of arguments. It may be raised
if
proc
returns something other than a single value. It will be raised if the
lists are of different lengths and one of the lists is fully
traversed, or if traversal reaches a suffix of one of the list
arguments which is either not a chain of pairs or is circular.
SEE ALSO
or(7scm),
and(7scm)
STANDARDS
R6RS
HISTORY
These procedures are new in R6RS. Similar procedures exist in SRFI-1
under the names
every
and
any.
AUTHORS
This page is part of the
scheme-manpages
project.
It includes materials from the RnRS documents.
More information can be found at
https://github.com/schemedoc/manpages/
.
Markup created by unroff 1.0sc, March 04, 2023.