I expected
-/ 1 2 3 4
_8 NB. ((1-2)-3)-4
but I got
-/ 1 2 3 4
_2 NB. 1-(2-(3-4))
I there a reason for this? How do I get the associativity that I was expecting? If there are relevant sections in the book, could you link them please?
On
It is also worthwhile to take a look at the Fold Conjunction group.
https://code.jsoftware.com/wiki/Vocabulary/fcap
It adds the sort of functionality that you are looking for, but with great power comes more complexity.
It's the 'rightmost first' rule you can read about in Part 1: Getting Acquainted, 1: Basics.
From section 1.5 "Parentheses":
So,
-/ 1 2 3 4resolves to1 - 2 - 3 - 4which by application of that rule is being interpreted as1 - (2 - (3 - 4)).As for your question, if there is a reason for it, the book argues in the same section that both, conventional associativity rules from "school mathematics" and this one, are "merely a convenience" to "reduce the number of parentheses we need to write". The advantage of resorting to this one in J should make computing a longer chain of functions explicitly intuitive by dropping the need to compare them according to an implicitly defined order that is visually unintuitive and needs to be remembered.
Later, in section 1.11 "Naming Scheme for Built-In Functions", you can find this very similar example, which uses
>.instead of-. Note that6 >. 5is evaluated first "by rightmost-first rule":To answer your question how to "get the associativity that I was expecting", think of what
((1 - 2) - 3) - 4is actually doing without the help of parentheses: You want to sum all items with all but the first one negated, i.e.1 + _2 + _3 + _4. So, use{.to get the head of the list (1), and}.to behead the list (2 3 4). Use-to negate the latter (producing_2 _3 _4), and@:to make it a fork, so we can use it with,to ravel both into a new list (1 _2 _3 _4), and compute its sum using+/: