private/fold.ss
#lang scheme/base
;; Fold combinators
(provide make-foldl make-foldr)

(define (make-foldl empty? first rest)
  (letrec ((f (lambda (cons empty ls)
                (cond [(empty? ls) empty]
                      [else (f cons
                               (cons (first ls) empty) 
                               (rest ls))]))))
    f))

(define (make-foldr empty? first rest)
  (letrec ((f (lambda (cons empty ls)
                (cond [(empty? ls) empty]
                      [else (cons (first ls)
                                  (f cons empty (rest ls)))]))))
    f))