If data =: 3 1 4
and frac =: % +/
, why does % +/ data
result in 0.125
but frac data
result in 0.375 0.125 0.5
?
1 Answer
%+/ 3 1 4
is "sum, then find reciprocal of that sum", that is:
+/ 3 1 4
8
% 8 NB. same as 1%8
0.125
But if you define frac =: %+/
, then %+/
becomes a group of two verbs isolated from their arguments (aka tacit definition), that is, a hook:
(%+/) 3 1 4
0.375 0.125 0.5
Which reads "sum, then divide original vector by that sum":
+/ 3 1 4
8
3 1 4 % 8
0.375 0.125 0.5
If you want frac
to behave as in the first example, then you need to either use an explicit definition:
frac =: 3 : '%+/y'
frac 3 1 4
0.125
Or to compose %
and +/
, e.g. with atop conjunction or clever use of dyadic fork with capped left branch:
%@(+/) 3 1 4
0.125
([:%+/) 3 1 4
0.125