chickadee » srfi-152 » string-fold-right

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

These are the fundamental iterators for strings.

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

(… (kons string[2] (kons string[1] (kons string[0] knil))))

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

(string-fold kons knil string start end)

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

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

(kons string[0]
      (… (kons string[end-3]
                 (kons string[end-2]
                       (kons string[end-1]
                             knil)))))

obeying the (tail) recursion

(string-fold-right kons knil string start end)

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

Examples:

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

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

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