在 J 中定义连词时出现奇怪的长度错误
我正在玩 J 中的副词和连词,遇到了一个奇怪的问题。我定义了一个名为 persistence
的简单副词,可用于查看在计算数字的数字乘积或数字和时生成的数字的级数。
S =: 1 : 'u/@:("."0)@":^:a:"0'
+ S 234
得到 234 9
。然后我用它创建了另一个副词来计算数字的持久性。
P =: 1 : '<:@#@(u S)"0'
+ P 234
给我们 1
。现在,假设我们想要找到 30 以下的所有数字,且附加持久性为 2,然后查看由 S
为每个数字生成的列表,例如,
+ S I. 2 = + P i.30
这将生成以下列表:
19 10 1
28 10 1
29 11 2
到目前为止,一切顺利。现在,我想把它变成一个连词,它的左侧包含用于持久性的动词,右侧包含用于限制列表的数字。 (上面示例中的 2
。)这是我对该连词的定义:
Q =: 2 : 'u S I. n = u P'
如果我在 J 控制台中输入表达式 + Q 2
,我会得到以下结果:
+/@:("."0)@":^:a:"0 I. 2 = <:@#@(+/@:("."0)@":^:a:"0)"0
This is完全正确,如果我使用 i.30
等参数运行完整表达式,它可以正常工作:
+/@:("."0)@":^:a:"0 I. 2 = <:@#@(+/@:("."0)@":^:a:"0)"0 i.30
但是,当我输入表达式 + Q 2 i.30
时进入 J 控制台,我收到“长度错误”。为什么?! + Q 2
不是完全等同于 +/@:("."0)@":^:a:"0 I. 2 = <:@#@(+ /@:("."0)@":^:a:"0)"0
?
我完全被难住了。我错过了什么?我已经在定义中玩过等级了结合和之外我只是不明白。
I'm playing around with adverbs and conjunctions in J, and have run across a strange problem. I've defined a simple adverb called persistence
that can be used to view the progression of numbers generated when calculating the digital product or digital sum of a number.
S =: 1 : 'u/@:("."0)@":^:a:"0'
+ S 234
gives us 234 9
. I then used this to create another adverb that calculates the persistence of a number.
P =: 1 : '<:@#@(u S)"0'
+ P 234
gives us 1
. Now, imagine we want to find all numbers below 30 with additive persistence of 2 and then view the list generated by S
for each number, e.g.,
+ S I. 2 = + P i.30
This generates the following list:
19 10 1
28 10 1
29 11 2
So far, so good. Now, I wanted to take this and turn it into a conjunction, the left-hand side of which contains the verb to use for persistence and the right-hand side of which contains the number used to limit the list. (2
in the example above.) Here's my definition of that conjunction:
Q =: 2 : 'u S I. n = u P'
If I enter the expression + Q 2
into the J console, I get back the following:
+/@:("."0)@":^:a:"0 I. 2 = <:@#@(+/@:("."0)@":^:a:"0)"0
This is exactly correct, and if I run the full expression with an argument such as i.30
, it works fine:
+/@:("."0)@":^:a:"0 I. 2 = <:@#@(+/@:("."0)@":^:a:"0)"0 i.30
However, when I enter the expression + Q 2 i.30
into the J console, I get back a 'length error'. Why?! Isn't + Q 2
exactly equivalent to +/@:("."0)@":^:a:"0 I. 2 = <:@#@(+/@:("."0)@":^:a:"0)"0
?
I'm completely stumped. What am I missing? I've played around with rank both inside the definition of the conjunction and outside of it. I just don't get it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
+ Q 2
与您提供的表达式完全相同,但在表达式中使用它时,就好像它在括号中一样。一般来说,
fgh y
!=(fgh) y
。在后者中,fg h
定义了一个train。例如:您可以通过添加对 y 的引用来修复连词,如下所示:
+ Q 2
is exactly equivalent to the expression you provided, but when using it in an expression it is as if it is in parenthesis.In general
f g h y
!=(f g h) y
. In the latterf g h
defines a train. For example:You can fix your conjunction by adding a reference to y to it like this: