Re: array-fold, array-fold-right, and array-reduce
Bradley Lucier 23 Aug 2021 00:51 UTC
I began this discussion after reading Joe Marshall's blog post on
fold-right.
After looking over fold-{left|right} in Haskell, OCaml, and SRFI 1, I
believe that the definition of fold (fold-left) in SRFI 1 does not
conform to the general understanding of this function.
SRFI 179 followed SRFI 1's definition of fold to define array-fold.
And SRFI 179 seems to have screwed up the definition of array-reduce.
In any follow-up SRFI, I think I'd do something like
(array-foldl (lambda (accumulate next-element) ...) initial array)
(f (f (f ... (f (f init value_0) value_1)...) value_n-1) value_n)
(array-foldr (lambda (next-element accumulate) ...) array final)
(f value_0 (f value_1 (... (f value_n-1 (f value_n final)))))
(array-reduce (lambda (current-value next-element) ...) array)
Assumes that current-value and next-element have the same "type",
whatever that means.
The definition of array-reduce is made a bit easier because the array
argument always has elements.
Brad