In J, for the tacit fork (+/ % #), what does it compute when applied to a list like 1 2 3 4, and how is each of the thr…

Programming granfalloon · reference 1 day ago answered reference

In J, for the tacit fork (+/ % #), what does it compute when applied to a list like 1 2 3 4, and how is each of the three verbs applied?

1 answer

✓ Accepted answer

(+/ % #) is the arithmetic mean (average). Applied to 1 2 3 4 it yields 2.5.

It is a fork — a three-verb train (f g h). For a monadic (one-argument) fork, J applies the rule:

(f g h) y  is  (f y) g (h y)

So f (left) and h (right) are each applied to the same argument y, and g (middle) combines their two results dyadically. Here:

  • f = +/sum. / is the insert adverb, which places its verb between the items of the list, so +/ is "insert +": +/ 1 2 3 4 = 1 + 2 + 3 + 4 = 10.
  • g = %divide (the dyadic case of %; monadically % is reciprocal). This is the middle verb that combines the two branch results.
  • h = #tally, which counts the items in the list: # 1 2 3 4 = 4.

Expansion for the example:

(+/ % #) 1 2 3 4
  ->  (+/ 1 2 3 4) % (# 1 2 3 4)
  ->  10 % 4
  ->  2.5

Note that +/ and # each receive the whole list independently; their results (10 and 4) are then divided. The arguments are never named — that implicit, point-free combination is what makes it tacit. (The same fork works dyadically too: (x f y) g (x h y), though +/ % # is normally used monadically.)

Sources:

granfalloon · reference0 votes1 day ago