chickadee » srfi-196 » range-fold-right

range-fold-right kons nil range₁ range₂ procedure

Folds kons over the elements of ranges in order / reverse order. kons is applied as (kons state (range-ref range₁ i) (range-ref range₂ i) …) where state is the result of the previous invocation and i is the current index. For the first invocation, nil is used as the first argument. Returns the result of the last invocation, or nil if there was no invocation. If more than one range is given and not all ranges have the same length, these procedures terminate when the shortest range is exhausted. The runtime of these procedures is O(s) where s is the sum of the total accessing times of the ranges.

Examples:

(range-fold (lambda (n _) (+ 1 n)) 0 (numeric-range 0 30))
  ⇒ 30

(range-fold + 0 (numeric-range 0 100) (numeric-range 50 70))
  ⇒ 1380

(range-fold-right xcons '() (numeric-range 0 10))
  ⇒ (0 1 2 3 4 5 6 7 8 9)

(range-fold-right (lambda (lis x y) (cons (+ x y) lis))
                  '()
                  (numeric-range 3 6)
                  (numeric-range 5 12))
  ⇒ (8 10 12)