www.delorie.com/gnu/docs/guile/guile_446.html   search  
 
Buy GNU books!


Guile Reference Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

39.3.5 Fold, Unfold & Map

Scheme Procedure: fold kons knil lst1 lst2 ...
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, ....

Scheme Procedure: fold-right kons knil lst1 lst2 ...
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))),

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

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

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

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

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

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.

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

Scheme Procedure: for-each f lst1 lst2 ...
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.

Scheme Procedure: append-map f lst1 lst2 ...
Scheme Procedure: append-map! f lst1 lst2 ...
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.

Scheme Procedure: map! f lst1 lst2 ...
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.

Scheme Procedure: pair-for-each f lst1 lst2 ...
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.

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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003