1 ;; Left and right folds.
3 ;; Left fold (f (.. (f (f init x1) x2) ..) xn)
6 ;; f : Accumulator Element -> Accumulator
8 ;; xs : sequence of Elements x1 x2 .. xn
9 ;; return : Accumulator
12 (reduce f (f init (first xs)) (rest xs)))))
14 ;; Right fold (f x1 (f x2 (.. (f xn init)) ..))
15 ;; The natural implementation for `foldr` is not tail-recursive, and
16 ;; the one based on `reduce` constructs many intermediate functions, so we
17 ;; rely on efficient `nth` and `count`.
21 rec (fn* [f xs acc index]
24 (rec f xs (f (nth xs index) acc) (- index 1))))
28 ;; f : Element Accumulator -> Accumulator
30 ;; xs : sequence of Elements x1 x2 .. xn
31 ;; return : Accumulator
32 (rec f xs init (- (count xs) 1)))))