Q - recursion with /

In q, a general illustration for an operator over /is the implementation of a Fibonacci sequence

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

This does print the first 10 Fibonacci numbers, but it makes no sense regarding the definition of the operator overin this whitepaper (p. 8)

With two arguments, the second is a list, the function is called with the left argument as the first parameter and the first element of the right argument as the second parameter. Then, 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 rest of the list items.

So, in the example with the fibonacci above, at the first iteration, the function will be called using [10;1]("the first parameter and the first element of the second parameter"), which would already give the wrong result.

My implementation matches the definition (and works)

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

but I don’t like it, because the parameter is cnever used.

How can the first example be reconciled with a definition?

thanks for the help

+4
source share
2 answers

/ has a number of forms described here: https://code.kx.com/q/ref/adverbs

The specific form used here is "repeat" - https://code.kx.com/q/ref/adverbs/#converge-repeat

The Fibonacci function is monadic here, i.e. takes one parameter (x)

/ (.. 1 1), , , 10 ( /)

, (.. )

,

AquaQ Analytics

0

- ().

. coverge-repeat code.kx.com .

+1

Source: https://habr.com/ru/post/1687708/


All Articles