q - 递归 /

q - recursion with /

在 q 中,over 运算符 / 的常见示例是 implementation of fibonacci sequence

10 {x,sum -2#x}/ 1 1

这确实打印了前 10 个斐波纳契数,但对于此 whitepaper(第 8 页)

over 运算符的定义没有意义

With two arguments, the second one being a list, the function is called with the left argument as its first parameter and the first element of the right argument as the second parameter. Next, the function is called with the result of the previous iteration as the first parameter and the third element as the second parameter. The process continues in this way for the remaining elements of the list.

所以在上面的斐波那契示例中,在第一次迭代中将使用 [10;1] ("first parameter and first item of second parameter") 调用函数,这已经给出了不正确的结果。

我的实现符合定义(并且有效)

1 1 {[l;c] l,sum -2#l}/til 10

但我不喜欢它,因为从未使用过参数 c

第一个例子如何与定义一致?

感谢您的帮助

左边的参数是这个例子被视为一个迭代整数原子(repeat)。

请参阅 coverge-repeat 上的 code.kx.com 可能会添加进一步的说明。

/ 有多种形式,这里描述:https://code.kx.com/q/ref/adverbs

这里使用的具体形式是 "repeat" - https://code.kx.com/q/ref/adverbs/#converge-repeat

这里的斐波那契函数是一元函数,即接受一个参数 (x)

在这种形式中,/ 将 运行 具有提供的参数的函数(即 1 1 的列表),然后是该调用的结果,然后再次是该调用的结果和所以,直到迭代次数达到10(/的左参数)

如果函数是二元函数(即采用两个参数),则您提供的定义为真

希望对您有所帮助

乔纳森

AquaQ 分析