chickadee » srfi-135 » textual-fold-right

(textual-fold kons knil textual [start end]) → valueprocedure
(textual-fold-right kons knil textual [start end]) → valueprocedure

These are the fundamental iterators for texts.

The textual-fold procedure maps the kons procedure across the given text or string from left to right:

(... (kons textual[2] (kons textual[1] (kons textual[0] knil))))

In other words, textual-fold obeys the (tail) recursion

(textual-fold kons knil textual start end) = (textual-fold kons (kons textual[start] knil) start+1 end)

The textual-fold-right procedure maps kons across the given text or string from right to left:

(kons textual[0]
      (... (kons textual[end-3]
                 (kons textual[end-2]
                       (kons textual[end-1] knil)))))

obeying the (tail) recursion

(textual-fold-right kons knil textual start end) = (textual-fold-right kons (kons textual[end-1] knil) start end-1)

Examples:

;; Convert a text or string to a list of chars.
(textual-fold-right cons '() textual)

;; Count the number of lower-case characters in a text or string.
(textual-fold (lambda (c count)
                (if (char-lower-case? c) (+ count 1) count))
              0
              textual)

The textual-fold-right combinator is sometimes called a "catamorphism."