Node:SRFI-1 Fold and Map, Next:, Previous:SRFI-1 Length Append etc, Up:SRFI-1

#### 39.3.5 Fold, Unfold & Map

 fold kons knil lst1 lst2 ... Scheme Procedure
 Fold the procedure kons across all elements of lst1, lst2, .... Produce the result of ```(kons en1 en2 ... (kons e21 e22 (kons e11 e12 knil)))```, if enm are the elements of the lists lst1, lst2, ....

 fold-right kons knil lst1 lst2 ... Scheme Procedure
 Similar to `fold`, but applies kons in right-to-left order to the list elements, that is: ```(kons e11 e12(kons e21 e22 ... (kons en1 en2 knil)))```,

 pair-fold kons knil lst1 lst2 ... Scheme Procedure
 Like `fold`, but apply kons to the pairs of the list instead of the list elements.

 pair-fold-right kons knil lst1 lst2 ... Scheme Procedure
 Like `fold-right`, but apply kons to the pairs of the list instead of the list elements.

 reduce f ridentity lst Scheme Procedure
 `reduce` is a variant of `reduce`. If lst is `()`, ridentity is returned. Otherwise, ```(fold (car lst) (cdr lst))``` is returned.

 reduce-right f ridentity lst Scheme Procedure
 This is the `fold-right` variant of reduce.

 unfold p f g seed [tail-gen] Scheme Procedure
 `unfold` is defined as follows: ```(unfold p f g seed) = (if (p seed) (tail-gen seed) (cons (f seed) (unfold p f g (g seed)))) ``` p Determines when to stop unfolding. f Maps each seed value to the corresponding list element. g Maps each seed value to next seed valu. seed The state value for the unfold. tail-gen Creates the tail of the list; defaults to `(lambda (x) '())`. g produces a series of seed values, which are mapped to list elements by f. These elements are put into a list in left-to-right order, and p tells when to stop unfolding.

 unfold-right p f g seed [tail] Scheme Procedure
 Construct a list with the following loop. ```(let lp ((seed seed) (lis tail)) (if (p seed) lis (lp (g seed) (cons (f seed) lis)))) ``` p Determines when to stop unfolding. f Maps each seed value to the corresponding list element. g Maps each seed value to next seed valu. seed The state value for the unfold. tail-gen Creates the tail of the list; defaults to `(lambda (x) '())`.

 map f lst1 lst2 ... Scheme Procedure
 Map the procedure over the list(s) lst1, lst2, ... and return a list containing the results of the procedure applications. This procedure is extended with respect to R5RS, because the argument lists may have different lengths. The result list will have the same length as the shortest argument lists. The order in which f will be applied to the list element(s) is not specified.

 for-each f lst1 lst2 ... Scheme Procedure
 Apply the procedure f to each pair of corresponding elements of the list(s) lst1, lst2, .... The return value is not specified. This procedure is extended with respect to R5RS, because the argument lists may have different lengths. The shortest argument list determines the number of times f is called. f will be applied to the list elements in left-to-right order.

 append-map f lst1 lst2 ... Scheme Procedure append-map! f lst1 lst2 ... Scheme Procedure
 Equivalent to ```(apply append (map f clist1 clist2 ...)) ``` and ```(apply append! (map f clist1 clist2 ...)) ``` Map f over the elements of the lists, just as in the `map` function. However, the results of the applications are appended together to make the final result. `append-map` uses `append` to append the results together; `append-map!` uses `append!`. The dynamic order in which the various applications of f are made is not specified.

 map! f lst1 lst2 ... Scheme Procedure
 Linear-update variant of `map` - `map!` is allowed, but not required, to alter the cons cells of lst1 to construct the result list. The dynamic order in which the various applications of f are made is not specified. In the n-ary case, lst2, lst3, ... must have at least as many elements as lst1.

 pair-for-each f lst1 lst2 ... Scheme Procedure
 Like `for-each`, but applies the procedure f to the pairs from which the argument lists are constructed, instead of the list elements. The return value is not specified.

 filter-map f lst1 lst2 ... Scheme Procedure
 Like `map`, but only results from the applications of f which are true are saved in the result list.