t3x.org / sketchy / library / fold-left.html
SketchyLISP
Reference
  Copyright (C) 2007
Nils M Holm

fold-left

Conformance: R5.91RS Scheme

Purpose: Iterate over lists. Combine the base element b with the list of the first members of each given list. Combine the result with the list of their second members, etc. When the given lists are empty, return the base element.
All lists passed to fold-left must have the same length.

Arguments:
F - function to apply
B - base element
A* ... - lists

Implementation:

(define (fold-left f b . a*)
  (letrec
    ((carof
       (lambda (a)
         (map-car car a)))
     (cdrof
       (lambda (a)
         (map-car cdr a)))
     (fold
       (lambda (a* r)
         (cond ((null? (car a*)) r)
           (else (fold (cdrof a*)
                   (apply f r (carof a*))))))))
    (cond ((null? a*)
        (bottom '(too few arguments to fold-left)))
      ((null? (car a*)) b)
      (else (fold a* b)))))

Example:

:l lib/list.scm
(fold-left list '1 '(a b c) '(d e f)) 
=> (((1 a d) b e) c f)

See also:
fold-right, map, member, list?.