Guides/Language FAQ/Atop Rank

From J Wiki
Jump to navigation Jump to search

Why does "+/@*: 1 2 3" give me 1 4 9 instead of 14?

You have

   +/ *: 1 2 3
14

and the Dictionary says that u@v y is u v y.
So how come?

   +/@*: 1 2 3
1 4 9

The problem is the rank of the verb. u@v creates a verb whose rank is the rank of v. This verb is equivalent to u v y but it is applied to each cell of y independently. The rank of +/@*: is the same as the rank of *:, i. e. 0; so +/ *: y happens for each scalar in y independently, rather than summing across the scalars.

The solution is to make sure the combined verb has infinite rank, so it will be applied to the entire y operand. The preferred way to do that is by using @: rather than @ as in

   +/@:*: 1 2 3
14


Contributed by HenryRich