chickadee » srfi-1 » pair-fold

pair-fold kons knil clist_1 clist_2 ...procedure

Analogous to fold, but KONS is applied to successive sublists of the lists, rather than successive elements -- that is, KONS is applied to the pairs making up the lists, giving this (tail) recursion:

(pair-fold KONS KNIL LIS) = (let ((tail (cdr LIS)))
                              (pair-fold KONS (KONS LIS KNIL) tail))
(pair-fold KONS KNIL {{'()}}) = KNIL

For finite lists, the KONS function may reliably apply set-cdr! to the pairs it is given without altering the sequence of execution.


;;; Destructively reverse a list.
(pair-fold (lambda (pair tail) (set-cdr! pair tail) pair) '() lis))

At least one of the list arguments must be finite.