函数和模的 J 语法

发布于 2024-12-12 19:19:28 字数 244 浏览 2 评论 0原文

我正在尝试创建一个返回输入模三的函数 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 技术交流群。

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

发布评论

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

评论(3

醉殇 2024-12-19 19:19:28

当使用 & (键)将名词绑定到动词时,它本质上是创建一个带有“固定”左(或右)参数的新动词。由于乘法是可交换的,因此无论将 2 固定为左参数还是右参数都无关紧要,因此以下内容是等效的:

   double1= *&2     NB. new verb "times by 2"
   double2=: 2&*    NB. new verb "2 times"
   double1 4
8
   double2 4
8

但是,residule (|) 不是可交换的,因此在根据您的情况,您必须确保修复/绑定(3 作为| 的左侧参数以获得所需的结果(数字除以3 的余数)。

   modulo3=: 3&|    NB. new verb "remainder after divison by 3"
   modulox=: |&3    NB. new verb "remainder of 3 divided by"
   modulo3 7
1
   modulox 7
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 the 2 as the left or right argument so the following are equivalent:

   double1= *&2     NB. new verb "times by 2"
   double2=: 2&*    NB. new verb "2 times"
   double1 4
8
   double2 4
8

However residule (|) is not commutative so in your case you must make sure you fix/bond (the 3 as the left argument of | to get the desired result (the remainder of a number divided by 3).

   modulo3=: 3&|    NB. new verb "remainder after divison by 3"
   modulox=: |&3    NB. new verb "remainder of 3 divided by"
   modulo3 7
1
   modulox 7
3
愁杀 2024-12-19 19:19:28

我不确定为什么 J(一种主要前缀语言)使用此语法,但邮件列表说使用此版本,并且它有效。

mod3 =: 3 & |

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.

mod3 =: 3 & |
初吻给了烟 2024-12-19 19:19:28

在我看来,关键在于,当你有一个二元动词,并且你将一个参数与它结合起来时,它就变成了一个一元动词。一元动词的论元总是位于 ay(J 术语)或右侧。

例子:
^&3]4
64
] 将 3 4 分开,这样它们就不会被视为单个数字。我从一个二元动词开始,power,取 x 的 y 次方。通过添加绑定,我创建了一个一元动词,正如它的定义一样,它取 y 的三次方。

    3&^ 4
81

这本质上是同一个例子,只不过现在我的一元动词是“取 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.

    3&^ 4
81

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. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文