如何将扫描的初始值设置为列表?
我正在尝试这样做:
(.5 .5) (+\) (.9 .2;.4 .1)
预期结果:
1.4 0.7
1.8 0.8
但我得到的是'type
。
我似乎无法使用列表作为最左边的参数。我做错了什么?
I'm trying to do:
(.5 .5) (+\) (.9 .2;.4 .1)
Expected result:
1.4 0.7
1.8 0.8
But instead I get 'type
.
I cannot seem to use a list as the leftmost argument. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有帕伦斯,您的表达式正常:
如您所见,您可以将列表作为左参数使用。但是标量扩展意味着您不需要。
解释
您需要从列表中的累积总和:
然后有些:
但是您已经看到了一些更深入的评价。
sums
是派生功能添加scan+\
的句法糖。派生函数为 variadic - 可以用作unary或a二进制 -   andsums
仅适用于单一应用程序。当添加扫描作为二进制时,其左参数是初始值。当正确的参数是一个长列表时,指定初始值比之后将其添加到每个累积总和之后更有效。
那些帕伦斯
如前所述,派生函数
+\
是variadic。要将其应用于单元,请使用括号符号
+\ [1 2 3]
或在Parens中“捕获”它。直接效果是防止其应用。您现在有一个数据项。它具有“名词语法”,可以作为参数传递。但是Q语法允许您通过并列使用名词。
对于名词
n
,n x
等效于n@x
或n [x]
。Your expression works fine without the parens:
As you see, you can use a list as left argument. But scalar extension means you need not.
Explanation
You want cumulative sums from your list:
And then some:
But you have seen a little deeper into this.
sums
is syntactic sugar for the derived function Add Scan+\
. The derived function is variadic – can be applied as either a unary or a binary – andsums
is only for unary application. When Add Scan is applied as a binary its left argument is an initial value.When the right argument is a long list, it is more efficient to specify an initial value than to add it afterwards to each of the cumulative sums.
Those parens
As mentioned, the derived function
+\
is variadic.To apply it as a unary, use either bracket notation
+\[1 2 3]
or ‘capture’ it in parens. The immediate effect is to prevent its application. You now have a data item. It has ‘noun syntax’ and can be passed as an argument.But q syntax allows you to apply a noun by juxtaposition.
For noun
N
,N x
is equivalent toN@x
orN[x]
.