Re: kons argument order in vector-map Bradley Lucier 30 Jan 2024 17:33 UTC

On 1/30/24 1:13 AM, Marc Nieper-Wißkirchen wrote:
>
> Am Di., 30. Jan. 2024 um 02:12 Uhr schrieb Bradley Lucier
> <xxxxxx@purdue.edu <mailto:xxxxxx@purdue.edu>>:
>
>     SRFI 1 has
>     ======================
>     (fold kons knil lis) = (fold kons (kons (car lis) knil) (cdr lis))
>     (fold kons knil '()) = knil
>     ======================
>     with the example
>     ======================
>     (fold cons '() lis)             ; Reverse LIS.
>     ======================
>     So the state, knil, is the rightmost argument (when there are multiple
>     list arguments to fold).
>
>     SRFI 133 says
>     ======================
>     kons is applied as (kons state (vector-ref vec1 i) (vector-ref vec2 i)
>     ...) where state is the current state value.
>     ======================
>     So the state is the leftmost argument.
>
>     I haven't gone through the SRFI 133 mail-list discussion, and there is
>     the example:
>     ======================
>     Produce a list of the reversed elements of vec:
>
>     (vector-fold (λ (tail elt) (cons elt tail)) '() vec)
>     ======================
>
>     So the argument order to vector-fold seems (a) reasonable as a choice,
>     and (b) incompatible with SRFI 1.
>
>     Am I understanding this correctly?
>
>
> Yes, this is correct.  There have been initiatives to change SRFI 1
> and/or SRFI 133 within R7RS-large.
>
> Marc

And SRFI 13 for strings says
======================
  The left-fold operator maps the kons procedure across the string from
left to right

(... (kons s[2] (kons s[1] (kons s[0] knil))))
======================
So this matches SRFI 1 and not SRFI 133.

Which is interesting because Gambit defines vector and string procedures
using big macros that just replace names of primitive operations, etc.,
in those procedures, e.g., 'vector- => 'string-.

So to use use this approach to define {vector|string}-fold{|-right} I'll
need to add an extra bit of complexity to the internal calls to kons to
distinguish between the two cases.

Not a complaint, but a note.

Brad