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

10.2.5 List fold, unfold & map

Function: fold kons knil clist1 clist2 ...
[SRFI-1] The fundamental list iterator. When it is given a single list clist1 = (e1 e2 ... en), then this procedure returns
 
(kons en ... (kons e2 (kons e1 knil)) ... ) 

If n list arguments are provided, then the kons function must take n+1 parameters: one element from each list, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest list runs out of values. At least one of the list arguments must be finite.

Examples:
 
(fold + 0 '(3 1 4 1 5 9)) => 23 ;sum up the elements
(fold cons '() '(a b c d e)) => (e d c b a) ;reverse
(fold cons* '() '(a b c) '(1 2 3 4 5))
    => (c 3 b 2 a 1) ;n-ary case

Function: fold-right kons knil clist1 clist2 ...
[SRFI-1] The fundamental list recursion operator. When it is given a single list clist1 = (e1 e2 ... en), then this procedure returns
 
(kons e1 (kons e2 ... (kons en knil)))

If n list arguments are provided, then the kons function must take n+1 parameters: one element from each list, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest list runs out of values. At least one of the list arguments must be finite.

Examples:
 
(fold-right cons '() '(a b c d e))
   => (a b c d e) ;copy list
(fold-right cons* '() '(a b c) '(1 2 3 4 5))
   => (a 1 b 2 c 3) ;n-ary case

Function: pair-fold kons knil clist1 clist2 ...
Function: pair-fold-right kons knil clist1 clist2 ...
[SRFI-1] Like fold and fold-right, but the procedure kons gets each cdr of the given clists, instead of car.

Function: reduce f ridentity list
Function: reduce-right f ridentity list
[SRFI-1] Variant of fold and fold-right. F must be a binary operator, and ridentity is the value such that for any value x that is valid as f's input,
 
 (f x ridentity) == ridentity

These functions effectively do the same thing as fold or fold-right, respectively, but omit application of f when list contains exactly one element, using the nature of ridentity.

Function: unfold p f g seed &optional tail-gen
[SRFI-1] Fundamental recursive list constructor. Defined by the following recursin.

 
(unfold p f g seed tail-gen) ==
   (if (p seed)
       (tail-gen seed)
       (cons (f seed)
             (unfold p f g (g seed))))
That is, p determines where to stop, g is used to generate successive seed value from the current seed value, and f is used to map each seed value to a list element.

Function: unfold-right p f g seed &optional tail
[SRFI-1] Fundamental iterative list constructor. Defined by the following recursin.

 
(unfold-right p f g seed tail) ==
  (let lp ((seed seed) (lis tail))
    (if (p seed)
        lis
        (lp (g seed) (cons (f seed) lis))))

Function: append-map f clist1 clist2 ...
Function: append-map! f clist1 clist2 ...
[SRFI-1] Equivalent to
 
  (apply append (map f clist1 clist2 ...))
  (apply append! (map f clist1 clist2 ...))
At least one of the list arguments must be finite.

Function: map! f list1 clist2 ...
[SRFI-1] The procedure f is applied to each element of list1 and corresponding elements of clists, and the result is collected to a list. Celss in list1 is reused to construct the result list.

Function: map-in-order f clist1 clist2 ...
[SRFI-1] A variant of map, but it guarantees to apply f on each elements of arguments in a left-to-right order. Since Gauche's map implementation follows the same order, this function is just a synonym of map.

Function: pair-for-each f clist1 clist2 ...
[SRFI-1] Like for-each, but the procedure f is applied on each cdr of clists.

Function: filter-map f clist1 clist2 ...
[SRFI-1] Like map, but only true values are saved. At least one of the list arguments must be finite.
 
(filter-map (lambda (x) (and (number? x) (* x x)))
            '(a 1 b 3 c 7))
  => (1 9 49)

Function: fold$ kons &optional knil
Function: fold-right$ kons &optional knil
Function: reduce$ f &optional ridentity
Function: reduce-right$ f &optional ridentity
Partial application versions of fold, fold-right, reduce, and reduce-right.


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

This document was generated by Ken Dickey on November, 28 2002 using texi2html