J:为什么`f^:proposition^:_y` 代表while 循环? [英] J: Why does `f^:proposition^:_ y` stand for a while loop?

查看:30
本文介绍了J:为什么`f^:proposition^:_y` 代表while 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我不明白为什么 f^:proposition^:_ y 是一个 while 循环.我实际上已经使用过它几次,但我不明白它是如何工作的.我知道 ^: 重复函数,但我对它在该语句中的双重使用感到困惑.

As title says, I don't understand why f^:proposition^:_ y is a while loop. I have actually used it a couple times, but I don't understand how it works. I get that ^: repeats functions, but I'm confused by its double use in that statement.

我也不明白为什么 f^:proposition^:a: y 有效.这与前一个相同,但返回所有迭代的值,而不是像上面的那样只返回最后一个.

I also can't understand why f^:proposition^:a: y works. This is the same as the previous one but returns the values from all the iterations, instead of only the last one as did the one above.

a: 是一个空框,我知道它具有与 ^: 一起使用的特殊含义,但即使在查看字典后我也无法理解.

a: is an empty box and I get that has a special meaning used with ^: but even after having looked into the dictionary I couldn't understand it.

谢谢.

推荐答案

f^:proposition^:_ 不是 while 循环.当proposition 返回10 时,它是(几乎) 一个while 循环.当 proposition 返回其他结果时,这是某种奇怪的 while 循环.

f^:proposition^:_ is not a while loop. It's (almost) a while loop when proposition returns 1 or 0. It's some strange kind of while loop when proposition returns other results.

让我们来看一个简单的 monadic 案例.

Let's take a simple monadic case.

f =: +:        NB. Double
v =: 20 > ]    NB. y less than 20

(f^:v^:_) 0     NB. steady case
0
(f^:v^:_) 1     NB. (f^:1) y, until (v y) = 0
32
(f^:v^:_) 2
32
(f^:v^:_) 5
20
(f^:v^:_) 21   NB. (f^:0) y
21

这就是正在发生的事情:每当 v y1 时,(f^:1) y 就会被执行.(f^:1) y 的结果是新的 y 等等.

This is what's happening: every time that v y is 1, (f^:1) y is executed. The result of (f^:1) y is the new y and so on.

  • 如果 y 连续两次保持不变 →输出 y 并停止.
  • 如果 v y0→输出 y 并停止.
  • If y stays the same for two times in a row → output y and stop.
  • If v y is 0→ output y and stop.

所以 f^:v^:_ 在这里,就像 double 而小于 20(或者直到结果没有改变)

So f^:v^:_ here, works like double while less than 20 (or until the result doesn't change)

让我们看看当 v 返回 2/0 而不是 1/0<时会发生什么.

Let's see what happens when v returns 2/0 instead of 1/0.

 v =: 2 * 20 > ]

(f^:v^:_) 0      NB. steady state
0
(f^:v^:_) 1      NB. (f^:2) 1 = 4 -> (f^:2) 4 = 16 -> (f^:2) 16 = 64 [ -> (f^:0) 64 ]
64
(f^:v^:_) 2      NB. (f^:2) 2 = 8 -> (f^:2) 8 = 32 [ -> (f^:0) 32 ]
32
(f^:v^:_) 5      NB. (f^:2) 5 = 20 [ -> (f^:0) 20 ]
20
(f^:v^:_) 21     NB. [ (f^:0) 21 ]
21

通过玩v,你可以有多种奇怪"的循环.(它甚至可以返回负整数,使用 f 的倒数).

You can have many kinds of "strange" loops by playing with v. (It can even return negative integers, to use the inverse of f).

这篇关于J:为什么`f^:proposition^:_y` 代表while 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆