Re: SRFI-1's fold is not fold-left! Peter Kourzanov 03 Nov 2012 12:17 UTC
Hallo again,

I don't know how many people actually use this stuff, but since I have
pressed on the "send" button I have realized that PAIR-FOLD was not
even trying to do the left-associative pair-fold:

(pair-fold [lambda (x y)
(cons (reduce + 0 x) y)]
'() '(1 2 3))
-> (3 5 6)

which is ((+ 3) (+ 2 3) (+ 1 2 3)): clearly right-associative
w.r.t the input list.

BTW, the same as:

(pair-fold-right [lambda (x y)
  (append y (list (reduce + 0 x)))]
  '() '(1 2 3))
-> (3 5 6)

Attached is the updated patch that behaves as follows:

(pair-fold-left [lambda (y x)
  (append y (list (reduce + 0 x)))]
  '() '(1 2 3))
-> (1 3 6)

which is ((+ 1) (+ 1 2) (+ 1 2 3)): clearly left-associative
w.r.t. the input list.

Hopefully now the implementation should make more sense,

Kind regards,
Pjotr

On Wed, 2012-10-31 at 10:44 +0100, Peter Kourzanov wrote:
> Dear Manuel, et.al.,
>
> While translating some OCaml code I have come across the following
> discrepancy. Although it is known, it is worthwhile to mention it, I
> think. The SRFI-1's fold is not actually a fold-left (it is mentioned
> in passing in SRFI-1 itself that F's args are flipped w.r.t. MIT-Scheme
> and Haskell).
>
> To set things straight, I think, it would make sense to add a fold-left
> function to Bigloo (and maybe to the SRFI-1 itself). An example patch is
> attached (fold-left)
>
> Kind regards,
> Pjotr
>
> P.S. Also attached is a comparable patch for SLIB.