在列表上组合加法和除法动词

Composing addition and division verbs over lists

如果 data =: 3 1 4frac =: % +/,为什么 % +/ data 的结果是 0.125frac data 的结果是 0.375 0.125 0.5

%+/ 3 1 4是"sum, then find reciprocal of that sum",即:

   +/ 3 1 4
8
   % 8       NB. same as 1%8
0.125

但是如果你定义了frac =: %+/,那么%+/就变成了一组两个动词,从它们的参数中分离出来(又名默认定义),即hook:

   (%+/) 3 1 4
0.375 0.125 0.5

上面写着 "sum, then divide original vector by that sum":

   +/ 3 1 4
8
   3 1 4 % 8
0.375 0.125 0.5

如果您希望 frac 的行为与第一个示例中的一样,那么您需要使用显式定义:

   frac =: 3 : '%+/y'
   frac 3 1 4
0.125

或组成%+/,例如atop conjunction or clever use of dyadic fork with capped 左分支:

   %@(+/) 3 1 4
0.125
   ([:%+/) 3 1 4
0.125