— Scheme Procedure: **fold**` proc init lst1 ... lstN`

— Scheme Procedure:**fold-right**` proc init lst1 ... lstN`

— Scheme Procedure:

Apply

procto the elements oflst1...lstNto build a result, and return that result.Each

proccall is`(`

procelem1`...`

elemNprevious`)`

, whereelem1is fromlst1, throughelemNfromlstN.previousis the return from the previous call toproc, or the giveninitfor the first call. If any list is empty, justinitis returned.

`fold`

works through the list elements from first to last. The following shows a list reversal and the calls it makes,(fold cons '() '(1 2 3)) (cons 1 '()) (cons 2 '(1)) (cons 3 '(2 1) ⇒ (3 2 1)

`fold-right`

works through the list elements from last to first, ie. from the right. So for example the following finds the longest string, and the last among equal longest,(fold-right (lambda (str prev) (if (> (string-length str) (string-length prev)) str prev)) "" '("x" "abc" "xyz" "jk")) ⇒ "xyz"If

lst1throughlstNhave different lengths,`fold`

stops when the end of the shortest is reached;`fold-right`

commences at the last element of the shortest. Ie. elements past the length of the shortest are ignored in the otherlsts. At least onelstmust be non-circular.

`fold`

should be preferred over`fold-right`

if the order of processing doesn't matter, or can be arranged either way, since`fold`

is a little more efficient.The way

`fold`

builds a result from iterating is quite general, it can do more than other iterations like say`map`

or`filter`

. The following for example removes adjacent duplicate elements from a list,(define (delete-adjacent-duplicates lst) (fold-right (lambda (elem ret) (if (equal? elem (first ret)) ret (cons elem ret))) (list (last lst)) lst)) (delete-adjacent-duplicates '(1 2 3 3 4 4 4 5)) ⇒ (1 2 3 4 5)Clearly the same sort of thing can be done with a

`for-each`

and a variable in which to build the result, but a self-containedproccan be re-used in multiple contexts, where a`for-each`

would have to be written out each time.

— Scheme Procedure: **pair-fold**` proc init lst1 ... lstN`

— Scheme Procedure:**pair-fold-right**` proc init lst1 ... lstN`

— Scheme Procedure:

The same as

`fold`

and`fold-right`

, but applyprocto the pairs of the lists instead of the list elements.

— Scheme Procedure: **reduce**` proc default lst`

— Scheme Procedure:**reduce-right**` proc default lst`

— Scheme Procedure:

`reduce`

is a variant of`fold`

, where the first call toprocis on two elements fromlst, rather than one element and a given initial value.If

lstis empty,`reduce`

returnsdefault(this is the only use fordefault). Iflsthas just one element then that's the return value. Otherwiseprocis called on the elements oflst.Each

proccall is`(`

procelemprevious`)`

, whereelemis fromlst(the second and subsequent elements oflst), andpreviousis the return from the previous call toproc. The first element oflstis thepreviousfor the first call toproc.For example, the following adds a list of numbers, the calls made to

`+`

are shown. (Of course`+`

accepts multiple arguments and can add a list directly, with`apply`

.)(reduce + 0 '(5 6 7)) ⇒ 18 (+ 6 5) ⇒ 11 (+ 7 11) ⇒ 18

`reduce`

can be used instead of`fold`

where theinitvalue is an “identity”, meaning a value which underprocdoesn't change the result, in this case 0 is an identity since`(+ 5 0)`

is just 5.`reduce`

avoids that unnecessary call.

`reduce-right`

is a similar variation on`fold-right`

, working from the end (ie. the right) oflst. The last element oflstis thepreviousfor the first call toproc, and theelemvalues go from the second last.

`reduce`

should be preferred over`reduce-right`

if the order of processing doesn't matter, or can be arranged either way, since`reduce`

is a little more efficient.

— Scheme Procedure: **unfold**` p f g seed `[`tail-gen`]

`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) '())`

.

gproduces a series of seed values, which are mapped to list elements byf. These elements are put into a list in left-to-right order, andptells when to stop unfolding.

— Scheme Procedure: **unfold-right**` p f g seed `[`tail`]

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) '())`

.

— Scheme Procedure: **map**` f lst1 lst2 ...`

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 whichfwill be applied to the list element(s) is not specified.

— Scheme Procedure: **for-each**` f lst1 lst2 ...`

Apply the procedure

fto 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 timesfis called.fwill be applied to the list elements in left-to-right order.

— Scheme Procedure: **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

fover 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

fare made is not specified.

— Scheme Procedure: **map!**` f lst1 lst2 ...`

Linear-update variant of

`map`

–`map!`

is allowed, but not required, to alter the cons cells oflst1to construct the result list.The dynamic order in which the various applications of

fare made is not specified. In the n-ary case,lst2,lst3, ... must have at least as many elements aslst1.