array-fold, array-fold-right, and array-reduce Bradley Lucier (19 Aug 2021 19:10 UTC)
Re: array-fold, array-fold-right, and array-reduce Alex Shinn (20 Aug 2021 00:27 UTC)
Re: array-fold, array-fold-right, and array-reduce Bradley Lucier (20 Aug 2021 00:58 UTC)
Re: array-fold, array-fold-right, and array-reduce Alex Shinn (20 Aug 2021 03:07 UTC)
Re: array-fold, array-fold-right, and array-reduce John Cowan (20 Aug 2021 20:31 UTC)
Re: array-fold, array-fold-right, and array-reduce Bradley Lucier (23 Aug 2021 00:51 UTC)

Re: array-fold, array-fold-right, and array-reduce Alex Shinn 20 Aug 2021 00:26 UTC

My preference would actually be to extend array-fold to allow multiple
arrays, like array-for-each. Then you can't put init last.

Also, although as Joe's post shows, when writing out all applications
of a fold-right as code the init is grouped with the final element
instead of the first, the visual order doesn't really line up either
way, because in your example fold-left does _not_ compute:

(f init (f a_11 (f a_10 (f a_01 a_00))))

Moreover, the init argument does not have the same semantics as the
elements.  It's an accumulated value of the same type as the result of
f, not the input elements.  Trying to consider it on the same level as
the elements may only confuse things.

Finally, "final" would not be an appropriate name because in either
fold it is still the _first_ value applied.

--
Alex

On Fri, Aug 20, 2021 at 4:10 AM Bradley Lucier <xxxxxx@math.purdue.edu> wrote:
>
> Joe Marshall discusses fold-left and fold-right here:
>
> http://funcall.blogspot.com/2021/08/fold-right.html
>
> His post has raised questions in my mind about whether SRFI 179 defines
> array-fold, array-fold-right, and array-reduce in The Right Way (TM).  I
> added a comment that notes:
>
> 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))))
>  > (array-fold-right (lambda (l r) `(f ,l ,r)) 'init a)
> (f a_00 (f a_01 (f a_10 (f a_11 init))))
>  > (array-reduce (lambda (l r) `(f ,l ,r)) a)
> (f (f (f a_00 a_01) a_10) a_11)
>
>
> These functions work the way they're documented, so I don't think any
> errata need to be applied to SRFI 179, but I'm wondering if this is
> another small thing that should be changed in a follow-up SRFI.
>
> Brad