简体   繁体   English

使用J在数组中查找

[英]Finding ones in an array with J

I will explain my problem in plain English and then show my attempts in J. 我会用简单的英语解释我的问题,然后展示我在J.的尝试。

Sum the indices of the 1's of a list of 1's and 0's and see if they equal another number. 将1和0的列表的1的索引求和,并查看它们是否等于另一个数字。 eg given 1 0 1 1 0 the indices are 0,2, and 3, and their sum is 5. So I can then test to see if it quals another number (obviously only true for 5 in this case). 例如,给定1 0 1 1 0,索引是0,2和3,并且它们的总和是5.因此我可以测试它是否可以算出另一个数字(在这种情况下,显然只有5)。

Here's my J: 这是我的J:

indexsumtest =: =+/I.
v =: 1 0 1 1 0
   5 indexsumtest v
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

What the? 什么? Here I assumed indexsumtest was a dyadic verb, maybe I need to explicitly put in x and y? 这里我假设indexsumtest是一个二元动词,也许我需要明确地输入x和y?

 indexsumtest =: x =+/I. y
   5 indexsumtest v
|value error: x
|   5     indexsumtest v

Nope. 不。 That made things worse. 这让事情变得更糟。

So I start from the beginning: 所以我从头开始:

I. v
0 2 3

Correct! 正确!

+/I. v
5

Correct again. 再次纠正。

5 =+/I. v
1

1 means true. 1意味着真实。 SO I did something right. 所以我做对了。

Why can't I compact these three operations into a single verb? 为什么我不能将这三个操作压缩成单个动词?

It is one that bites everyone now and then and it results because 它是一个偶尔咬人的结果,因为它

5 =+/I. v

actually has I. acting on v, then the result is acted on by +/, then that result is tested against 5 using the = comparison. 实际上我有一个作用于v,然后结果被+ /作用,然后使用=比较对照结果进行测试。 So when we define 所以当我们定义时

   indexsumtest =: =+/I.

we get 我们得到

      5 indexsumtest v
   0 0 0 0 0
   0 0 0 0 0
   0 0 0 0 0
   0 0 0 0 0
   0 0 0 0 0

because indexsumtest has been defined as a tacit verb which processes its arguments as a fork as MPelletier suggested. 因为indexsumtest已被定义为默认动词,它将其参数作为MPelletier建议的fork处理。 In other words you actually don't get =+/I. 换句话说,你实际上没有= + / I。 in place of indexsumtest you get (=+/I.) which is a 3 verb fork where the first and third verbs take both arguments and the result of each is sent to the +/ in the centre. 代替indexsumtest你得到(= + / I。)这是一个3动词叉,其中第一个和第三个动词同时接受两个参数,每个动词的结果被发送到中心的+ /。

MPelletier also suggests that a tacit form that does what you wanted is MPelletier还建议你做一个你想要的默契形式

[ = [: +/ [: I. ]

which will work and is actually created by J if you change your definition to 13 如果你将你的定义更改为13,它将工作并实际由J创建

indexsumtest =: 13 : 'x =+/I. y'
indexsumtest
[ = [: +/ [: I. ]

Yep, that is right J will actually do most tacit conversions for you if you use the 13 : conjunction to define. 是的,如果您使用13:连接来定义,那么J实际上将为您进行大多数默认转换。 Pretty cool, eh? 很酷,嗯? Only problem is that it does not use hooks in this automatic generation, which can simplify the code. 唯一的问题是它不会在这个自动生成中使用钩子,这可以简化代码。 A hook is a two verb combination that has the right verb work on the right argument and the result is used as the right argument to the left verb which uses the left argument in the dyadic case (or the original right argument in the monadic case). 一个钩子是一个双动词组合,它在右边的参数上有正确的动词作用,结果被用作左动词的右参数,它使用二元情形中的左参数(或monadic情形中的原始右参数) 。

When x and y are left and right arguments and u and v are the first and second verbs then 当x和y是左右参数时,u和v是第一个和第二个动词

x ( uv) y becomes xu (vy) x ( uv) y变为xu (vy)

or in monadic case 或者在一元情况下

( uv) y becomes yu (vy) ( uv) y成为yu (vy)

If you are still with me (good for you!) this means that we can use a hook to simplify the tacit if we set u to = and v to +/@I. 如果你还在我身边(对你有好处!)这意味着如果我们将你设置为=和v到+ / @我,我们可以使用一个钩来简化默认。 (the @ is a conjunction that joins +/ and I. and makes them work as one verb). (@是一个连接+ /和I的连接,并使它们作为一个动词起作用)。 So finally 最后

   indexsumtacit =: = +/@I.
   5 indexsumtacit v
1

As mindbending as J is when you start, it is really neat when you understand the rules that it plays by and then start to get them to do your bidding. 正如J开始时的心态,当你理解它所遵循的规则然后开始让它们进行你的出价时,它真的很整洁。 Hope this helps. 希望这可以帮助。

Consider the classic mean function: 考虑经典的平均函数:

mean =: +/%#
mean i.6

3 part declarations like that operate this way: 像这样的3部分声明以这种方式运行:

  • # is performed against y #y执行
  • +/ is performed against y +/是针对y执行的
  • % is performed dyadically against the results of +/ and # in their respective places %在各自的位置与+/#的结果进行二元对比

The principle is the same with your dyadic function: 原理与你的二元函数相同:

  • I. is performed against x and y I.针对xy执行
  • = is performed against x and y =xy执行
  • +/ is performed against the results of = and I. in their respective places +/在各自的位置对=I.的结果执行

So, doing 所以,做

indexsumtest =: =+/I.
5 indexsumtest 1 0 1 1 0

Is equivalent to 相当于

(5 = 1 0 1 1 0) +/ (5 I. 1 0 1 1 0)

Far from what you want. 远非你想要的。

The simple trick is to define a dyadic function explicitly: 简单的技巧是明确定义二元函数:

indexsumtest =: 4 : 'x =+/I. y'

J's tacit definition also suggests this: J的默认定义也表明了这一点:

[ = [: +/ [: I. ]

Which is a bit heavy for my taste. 这对我来说有点重。

It is all about how J parses an expression. 这是关于J如何解析表达式的全部内容。 When J sees a verb, it implicitly adds a parentheses around it. 当J看到一个动词时,它隐含地在它周围添加一个括号。 So 5 indexsumtest v is 5 (=+/I.) v ; 所以5 indexsumtest v5 (=+/I.) v ; while 5 =+/I. v 5 =+/I. v 5 =+/I. v is actually 5 =+/ (I. v) . 5 =+/I. v实际上是5 =+/ (I. v) These two are different as answers above. 这两个与上面的答案不同。

I found that it is extremely helpful to understand how J evaluates expressions by reading Appendix 1 of Learning J . 我发现通过阅读Learning J的附录1来理解J如何评估表达式是非常有帮助的。 This deserves a whole chapter rather than an appendix for any J tutorials. 对于任何J教程,这应该是整章而不是附录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM