简体   繁体   English

如何在J中的同一行写几个语句

[英]how to write several statements in the same line in J

I am a newbie and try to learn J. There is one question I have had for quite some time. 我是一个新手,并试着学习J.我有一个问题已经有一段时间了。

What is the statement separator in J ? J的语句分隔符是什么? Can I write several statements in the same line in J? 我可以在J的同一行写几个语句吗?

Although the statement separator in J is the linefeed, you can separate assignment statements on a single line by using the Left verb (dyadic [) which always returns its left argument. 虽然J中的语句分隔符是换行符,但您可以使用左侧动词(dyadic [)将单个赋值语句分隔在一行中,该动词始终返回其左侧参数。 Since J parses right to left, the Left verb effectively breaks up statements as the right argument is discarded (although side effects such as assignment still occur). 由于J从右向左解析,因此左侧动词有效地分解语句,因为正确的参数被丢弃(尽管仍然会发生分配等副作用)。

   2 + 3 [ t=. 3 + 4
5
  t
7

So in this case t is assigned 7 and then 3 [ t is evaluated returning 3 which is added to 2. In contrast if you use Right (dyadic ]) you may be swimming upstream with different results. 因此,在这种情况下,t被分配7然后3 [t被评估返回3被添加到2.相反,如果你使用Right(二元)),你可能在上游游泳,结果不同。

   2 + 3 ] t=. 6 + 4
12
   t
10

Here t is assigned 10 and the result of 3 ] t is t with a value of 10 which is then added to 2. 这里t被赋值为10,而3] t的结果是t,值为10,然后加到2。

Hope this helps, bob 希望这会有所帮助,鲍勃

There is no statement separator. 没有语句分隔符。 Or rather, the statement separator is a line feed. 或者更确切地说,语句分隔符是换行符。

As MPelletier notes, there is no statement operator but you can use the "assignment trick": assign the result of a statement to a variable when you use it, the first time (from right to left) that you use it. 正如MPelletier所指出的那样,没有语句运算符,但你可以使用“赋值技巧”:在你使用它时,将语句的结果分配给变量,第一次(从右到左)你使用它。 Eg: 例如:

Instead of this: 而不是这个:

a =: 15
f =: (+/) % # i.a
g =: (-/) % # i.a
h =: ((-:g) * (+:f)) ^ ((-:f) * (+:g))

you can write this: 你可以这样写:

h =: ((-:g) * (+:f)) ^ ((-: f =. (+/) % #i.a) * (+: g =. (-/) % # i.a=.15))

Or, instead of: 或者,而不是:

mean =: (+/) % #
f =: mean i.15

this: 这个:

f =: (mean =: (+/) % #) i.15

Alternative to Do ". , you can create an anonymous verb with Explicit Verb 3 : and invoke it. This way you don't need to worry about leaked temporary variables. 替代Do ". ,您可以使用Explicit Verb 3 :创建一个匿名动词3 :并调用它。这样您就不必担心泄漏的临时变量。

For multiple statements, you have some way: 对于多个语句,您有一些方法:

  1. Reverse statements and join with [ (ex. 'bar =. foo + 54 [ foo =. 42' ) 反向陈述并加入[ (例如'bar =. foo + 54 [ foo =. 42'
  2. Concat with line feeds (ex. 'foo =. 42' , LF , 'bar =. foo + 54' ) Concat与换行符(例如'foo =. 42' , LF , 'bar =. foo + 54'
  3. Shape code as a table (ex. 'foo =. 42' ,: 'bar =. foo + 54' ). 将形状代码作为表格(例如'foo =. 42' ,: 'bar =. foo + 54' )。

Borrowing Eelvex's example : 借用Eelvex的例子

   (3 : 'h =. ((-:g) * (+:f)) ^ ((-:f) * (+:g)) [ g =. (-/) % # i.a [ f =. (+/) % # i.a [ a =. 15') ''
0.976216
   (3 : ('a =. 15' ,LF, 'f =. (+/) % # i.a' ,LF, 'g =. (-/) % # i.a' ,LF, 'h =. ((-:g) * (+:f)) ^ ((-:f) * (+:g))')) ''
0.976216
   (3 : ('a =. 15' , 'f =. (+/) % # i.a' , 'g =. (-/) % # i.a' ,: 'h =. ((-:g) * (+:f)) ^ ((-:f) * (+:g))')) ''
0.976216

Somehow 3 : <...> gets evaluated before usual right-to-left execution, so you need parentheses for the right hand of : . 不知何故3 : <...>在通常的从右到左执行之前得到评估,所以你需要右括号的括号: Using this behavior, you can omit parentheses around 3 : <...> : 使用此行为,您可以省略3 : <...>左右的括号3 : <...>

   3 : '6 * 9' ''
54

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

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