t3x.org / sketchy / library / reduce.html
SketchyLISP
Reference
  Copyright (C) 2006
Nils M Holm

reduce

Conformance: SketchyLISP Core

Purpose: Iterate through a list. Combine the first member of the list with the second member, the result with the third member, etc. When the given list is empty, return a default.

Arguments:
F - combining function
A - list to reduce
DEFAULT - expr to return when A=()

Implementation:

(define (reduce f a default)
  (letrec
    ((_reduce (lambda (a res)
      (cond ((null? a) res)
        (#t (_reduce (cdr a)
              (f res (car a))))))))
    (cond ((null? a) default)
      (#t (_reduce (cdr a) (car a))))))

Example:

(reduce cons '(a b c d) #f) 
=> (((a . b) . c) . d)

See also:
map, reduce-r.