如何将扫描的初始值设置为列表?

发布于 2025-01-19 05:22:12 字数 188 浏览 1 评论 0原文

我正在尝试这样做:

(.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2025-01-26 05:22:12
q)(+\)[0.5 0.5;(.9 .2;.4 .1)]
1.4 0.7
1.8 0.8
q)(+\)[0.5 0.5;(.9 .2;.4 .1)]
1.4 0.7
1.8 0.8
凡间太子 2025-01-26 05:22:12

如果没有帕伦斯,您的表达式正常:

q).5 .5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

如您所见,您可以将列表作为左参数使用。但是标量扩展意味着您不需要。

q).5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

解释
您需要从列表中的累积总和:

q)sums (.9 .2;.4 .1)
0.9 0.2
1.3 0.3

然后有些:

q).5 + sums (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

但是您已经看到了一些更深入的评价。 sums是派生功能添加scan +\的句法糖。派生函数为 variadic - 可以用作unary或a二进制 -   and sums仅适用于单一应用程序。当添加扫描作为二进制时,其左参数是初始值。

q).5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

当正确的参数是一个长列表时,指定初始值比之后将其添加到每个累积总和之后更有效。

q)show L:1000000?1.
0.3927524 0.5170911 0.5159796 0.4066642 0.1780839 0.3017723 0.785033 0.5347096 0.711171..
q)\ts:1000 .5+sums L
2360 16777472
q)\ts:1000 .5+\ L
1477 8388880

那些帕伦斯
如前所述,派生函数+\是variadic。

q)5+\1 2 3  / +\ applied as a binary
6 8 11

要将其应用于单元,请使用括号符号+\ [1 2 3]或在Parens中“捕获”它。直接效果是防止其应用。您现在有一个数据项。它具有“名词语法”,可以作为参数传递。

q)type(+\)
108h

但是Q语法允许您通过并列使用名词。
对于名词nn x等效于n@xn [x]

q)"abc" 2 0
"ca"
q)til 3
0 1 2
q)(+\)1 2 3  / +\ applied as a unary
1 3 6

Your expression works fine without the parens:

q).5 .5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

As you see, you can use a list as left argument. But scalar extension means you need not.

q).5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

Explanation
You want cumulative sums from your list:

q)sums (.9 .2;.4 .1)
0.9 0.2
1.3 0.3

And then some:

q).5 + sums (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

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 – and sums is only for unary application. When Add Scan is applied as a binary its left argument is an initial value.

q).5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8

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.

q)show L:1000000?1.
0.3927524 0.5170911 0.5159796 0.4066642 0.1780839 0.3017723 0.785033 0.5347096 0.711171..
q)\ts:1000 .5+sums L
2360 16777472
q)\ts:1000 .5+\ L
1477 8388880

Those parens
As mentioned, the derived function +\ is variadic.

q)5+\1 2 3  / +\ applied as a binary
6 8 11

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.

q)type(+\)
108h

But q syntax allows you to apply a noun by juxtaposition.
For noun N, N x is equivalent to N@x or N[x].

q)"abc" 2 0
"ca"
q)til 3
0 1 2
q)(+\)1 2 3  / +\ applied as a unary
1 3 6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文