函数和模的 J 语法
我正在尝试创建一个返回输入模三的函数 mod3
,但我的语法是错误的。我不明白为什么语法与文档中的 double 示例有任何不同。
$ jconsole
double =: * & 2
double 1
2
double 2
4
double 3
6
mod3 =: 3 | &
|syntax error
| mod3=: 3|&
I'm trying to make a function mod3
that returns the input modulo three, but my syntax is wrong. I don't see why the syntax would be any different from the double
example in the docs.
$ jconsole
double =: * & 2
double 1
2
double 2
4
double 3
6
mod3 =: 3 | &
|syntax error
| mod3=: 3|&
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当使用
&
(键)将名词绑定到动词时,它本质上是创建一个带有“固定”左(或右)参数的新动词。由于乘法是可交换的,因此无论将2
固定为左参数还是右参数都无关紧要,因此以下内容是等效的:但是,residule (
|
) 不是可交换的,因此在根据您的情况,您必须确保修复/绑定(3
作为|
的左侧参数以获得所需的结果(数字除以3 的余数)。When
&
(bond) is used to bind a noun to a verb it is essentially creating a new verb with a "fixed" left (or right) argument. Because multiplication is commutative it doesn't matter whether you fix the2
as the left or right argument so the following are equivalent:However residule (
|
) is not commutative so in your case you must make sure you fix/bond (the3
as the left argument of|
to get the desired result (the remainder of a number divided by 3).我不确定为什么 J(一种主要前缀语言)使用此语法,但邮件列表说使用此版本,并且它有效。
I'm not sure why J, a predominately prefix language, uses this syntax, but the mailing list says to use this version, and it works.
在我看来,关键在于,当你有一个二元动词,并且你将一个参数与它结合起来时,它就变成了一个一元动词。一元动词的论元总是位于 ay(J 术语)或右侧。
例子:
^&3]4
64
] 将 3 4 分开,这样它们就不会被视为单个数字。我从一个二元动词开始,power,取 x 的 y 次方。通过添加绑定,我创建了一个一元动词,正如它的定义一样,它取 y 的三次方。
这本质上是同一个例子,只不过现在我的一元动词是“取 3 的 y 次方”。
双示例(更简洁为+:)试图展示的正是我们所展示的:转换为带键的一元动词的二元动词始终将其需要执行的单个参数作为正确的参数,不不管第一个参数绑定到哪一边。
它没有表明的是,对于不可交换的动词,将原始参数绑定到哪一边很重要。但现在你知道了。 :-)
The point, as I see it, is that when you have a dyadic verb, and you bond an argument to it, it becomes a monadic verb. Monadic verbs always have their argument as a y (J terminology) or, on the right.
Example:
^&3] 4
64
The ] separates the 3 4 so that they are not seen as a single number. I started with a dyadic verb, power, take x to the y power. By adding the bind, I have created a monadic verb that has, as it's definition, take y to the third power.
This is essentially the same example, except that now my monadic verb is "take 3 to the y power".
What the double example (more succinct as +:) was trying to show was exactly what we are showing: That a dyadic verb which is converted into a monadic verb with bond always takes the single argument it needs to execute as a right argument, no matter which side the first argument is bonded to.
What it does not show is that for verbs which are not commutative, it matters which side you bind the original argument to. But now you know. :-)