Re: array-fold, array-fold-right, and array-reduce

*Bradley Lucier* 20 Aug 2021 00:58 UTC

SRFI 1 says that
reduce f ridentity list -> value
reduce is a variant of fold.
ridentity should be a "right identity" of the procedure f -- that
is, for any value x acceptable to f,
(f x ridentity) = x
reduce has the following definition:
If list = (), return ridentity;
Otherwise, return (fold f (car list) (cdr list)).
...in other words, we compute (fold f ridentity list).
If the idea is that
(array-reduce f a)
is the same as
(reduce f (array->list a))
then we don't have that
heine:~/programs/gambit/gambit> gsi
Gambit v4.9.3-1503-gd1a6c6bf
> (import (srfi 179))
> (define a (make-array (make-interval '#(2 2))
(lambda (i j)
(string->symbol (string-append "a_"
(number->string i) (number->string j))))))
> (array-fold (lambda (l r) `(f ,l ,r)) 'init a)
(f a_11 (f a_10 (f a_01 (f a_00 init))))
> (fold (lambda (l r) `(f ,l ,r)) 'init (array->list a))
(f a_11 (f a_10 (f a_01 (f a_00 init))))
> (define (reduce f l) (fold f (car l) (cdr l))) ;; l will never be ()
> (reduce (lambda (l r) `(f ,l ,r)) (array->list a))
(f a_11 (f a_10 (f a_01 a_00)))
> (array-reduce (lambda (l r) `(f ,l ,r)) a)
(f (f (f a_00 a_01) a_10) a_11)
So the question seems to be:
Do we want
(array-reduce f a)
to produce the same result as
(reduce f (array->list a))
where reduce is defined in SRFI 1.
Which leads to the secondary question that we perhaps don't want to
answer (or even ask):
Is reduce defined correctly in SRFI 1?
Brad