Next: Miscellaneous List Operations, Previous: Mapping of Lists, Up: Lists

— procedure: **reduce-left**` procedure initial list`

Combines all the elements of

listusing the binary operationprocedure. For example, using`+`

one can add up all the elements:(reduce-left + 0 list-of-numbers)The argument

initialis used only iflistis empty; in this caseinitialis the result of the call to`reduce-left`

. Iflisthas a single argument, it is returned. Otherwise, the arguments are reduced in a left-associative fashion. For example:(reduce-left + 0 '(1 2 3 4)) => 10 (reduce-left + 0 '(1 2)) => 3 (reduce-left + 0 '(1)) => 1 (reduce-left + 0 '()) => 0 (reduce-left + 0 '(foo)) => foo (reduce-left list '() '(1 2 3 4)) => (((1 2) 3) 4)

— procedure: **reduce-right**` procedure initial list`

Like

`reduce-left`

except that it is right-associative.(reduce-right list '() '(1 2 3 4)) => (1 (2 (3 4)))

— procedure: **fold-right**` procedure initial list`

Combines all of the elements of

listusing the binary operationprocedure. Unlike`reduce-left`

and`reduce-right`

,initialis always used:(fold-right + 0 '(1 2 3 4)) => 10 (fold-right + 0 '(foo)) error--> Illegal datum (fold-right list '() '(1 2 3 4)) => (1 (2 (3 (4 ()))))

`Fold-right`

has interesting properties because it establishes a homomorphism between (`cons`

,`()`

) and (procedure,initial). It can be thought of as replacing the pairs in the spine of the list withprocedureand replacing the`()`

at the end withinitial. Many of the classical list-processing procedures can be expressed in terms of`fold-right`

, at least for the simple versions that take a fixed number of arguments:(define (copy-list list) (fold-right cons '() list)) (define (append list1 list2) (fold-right cons list2 list1)) (define (map p list) (fold-right (lambda (x r) (cons (p x) r)) '() list)) (define (reverse items) (fold-right (lambda (x r) (append r (list x))) '() items))

— procedure: **fold-left**` procedure initial list`

Combines all the elements of

listusing the binary operationprocedure. Elements are combined starting withinitialand then the elements oflistfrom left to right. Whereas`fold-right`

is recursive in nature, capturing the essence of`cdr`

-ing down a list and then computing a result,fold-leftis iterative in nature, combining the elements as the list is traversed.(fold-left list '() '(1 2 3 4)) => ((((() 1) 2) 3) 4) (define (length list) (fold-left (lambda (sum element) (+ sum 1)) 0 list)) (define (reverse items) (fold-left (lambda (x y) (cons y x)) () items))

— procedure: **any**` predicate list list ...`

(SRFI 1) Applies

predicateacross thelists, returning true ifpredicatereturns true on any application.If there are n list arguments

list1...listn, thenpredicatemust be a procedure taking n arguments and returning a boolean result.

`any`

appliespredicateto the first elements of thelistparameters. If this application returns a true value,`any`

immediately returns that value. Otherwise, it iterates, applyingpredicateto the second elements of thelistparameters, then the third, and so forth. The iteration stops when a true value is produced or one of the lists runs out of values; in the latter case,`any`

returns`#f`

. The application ofpredicateto the last element of thelists is a tail call.Note the difference between

`find`

and`any`

—`find`

returns the element that satisfied the predicate;`any`

returns the true value that thepredicateproduced.Like

`every`

,`any`

's name does not end with a question mark—this is to indicate that it does not return a simple boolean (`#t`

or`#f`

), but a general value.(any integer? '(a 3 b 2.7)) => #t (any integer? '(a 3.1 b 2.7)) => #f (any < '(3 1 4 1 5) '(2 7 1 8 2)) => #tThe non-standard procedure

`there-exists?`

is similar, except that it takes a single list and a predicate argument, in that order.

— procedure: **every**` predicate list list ...`

(SRFI 1) Applies

predicateacross thelists, returning true ifpredicatereturns true on every application.If there are n list arguments

list1...listn, thenpredicatemust be a procedure taking n arguments and returning a boolean result.

`every`

appliespredicateto the first elements of thelistparameters. If this application returns false,`every`

immediately returns false. Otherwise, it iterates, applyingpredicateto the second elements of thelistparameters, then the third, and so forth. The iteration stops when a false value is produced or one of thelists runs out of values. In the latter case,`every`

returns the true value produced by its final application ofpredicate. The application ofpredicateto the last element of thelists is a tail call.If one of the

lists has no elements,`every`

simply returns`#t`

.Like

`any`

,`every`

's name does not end with a question mark—this is to indicate that it does not return a simple boolean (`#t`

or`#f`

), but a general value.The non-standard procedure

`for-all?`

is similar, except that it takes a single list and a predicate argument, in that order.