SketchyLISP Reference |
Copyright (C) 2007 Nils M Holm |
<<[fold-left] | [Index] | [gcd]>> |
Conformance: R5.91RS Scheme
Purpose:
Iterate over lists right-associatively. Combine the list
of last members of the given lists with the base element
b.
Combine the result with the list of second-to-last
members, etc.
When the given lists are empty, return the base
element.
All lists passed to
fold-right
must have the same length.
Arguments:
F - function to apply
B - base element
A* ... - lists
Implementation:
(define (fold-right f b . a*) (letrec ((carof (lambda (a) (map-car car a))) (cdrof (lambda (a) (map-car cdr a))) (foldr (lambda (a* r) (cond ((null? (car a*)) r) (else (foldr (cdrof a*) (apply f (append (carof a*) (list r))))))))) (cond ((null? a*) (bottom '(too few arguments to fold-right))) ((null? (car a*)) b) (else (foldr (map reverse a*) b)))))
Example:
:l lib/list.scm (fold-right list '9 '(a b c) '(d e f)) => (a d (b e (c f 9)))
See also:
fold-left,
map,
member,
list?.
<<[fold-left] | [Index] | [gcd]>> |