On Tue, Feb 23, 2021 at 3:37 PM Hauke Rehr <[email protected]> wrote:
> Folds are a variation on /
> There’s not much more to it.
My thinking is this:
For dyadic uses of folds, the left argument to the verb derived by
fold should be the initial value for the sequence.
Meanwhile, there's two kinds of folds. Right to left folds and left to
right folds.
Right to left folds (F.: is like / and F:: is like /\.). For those
folds, it makes sense that the initial value be the right hand
argument to the first invocation of the fold's v argument.
Left to right folds (F.. and F:.) do not have corresponding primitive
adverb. For these folds, it makes sense that the initial value be the
left hand argument to the first invocation of the fold's v argument.
In other words:
V=: {{x+y [ echo x,y}}
7 ]F.:V 1 2 3 4
4 7
3 11
2 14
1 16
17
This is perfect. 7 is the initial value, and you get the same result
from this expression:
1 V 2 V 3 V 4 V 7
4 7
3 11
2 14
1 16
17
It's just like inserting V between each of the items in the list, and
tacking 7 on as the initial value.
Or, to emphasize the right to left nature of that expression, we could
put in the implied parenthesis:
1 V(2 V(3 V(4 V 7)))
4 7
3 11
2 14
1 16
17
-------------
For the left to right folds, we do basically the same thing, but we
would have to use parentheses to force left to right evaluation. In
other words, we should expect that
7 ]F..V 1 2 3 4
works like this:
(((7 V 1)V 2)V 3)V 4
7 1
8 2
10 3
13 4
17
And, in my earlier tests today, I thought I was getting that result.
But I see now that I am not.
7]F..V 1 2 3 4
1 7
2 8
3 10
4 13
17
Anyways, for the F.. and F:. verbs, I think it does make sense to
change the fold implementation, so that the 'initial' or 'previous'
value appears on the left.
And, looking back on what Sergy wrote, it looks like I was reading him wrong.
Thanks,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm