times or start value

hi guys.
when I write 

10 {x+y}/ 1 2

the 10 means it is the start value,

but when I write

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

the 10 means 10 times

my question is how to tell when it means start value and when it means recursion times?  

Hi Leslie,

It is determined by the rank (or valence) of the function in question. See here: https://code.kx.com/v2/ref/accumulators, specifically the section on unary and binary values.

{x+y} is binary, so the first evaluation applies to the left argument and the first item of the right argument (10 and 1) in your case. The result of this becomes the left argument of the next iteration, and the second item of the right hand argument becomes the new right argument.

{ x, sum -2#x} is unary. The 10 here is interpreted as do 10 times, however you are not strictly limited to a number on the left hand side here. You can use this syntax to repeat a function until it converges or while some condition is true. e.g:

({ x, sum -2#x}/)1 1 will run until the function converges. (This function will never converge however)

(20>count@){ x, sum -2#x}/1 1 will run while the count of the output from the iteration in question is less than 20.

Regards,

Cillian

using ‘/’(over) or ''(scan) with a monadic function (only including an ‘x’ parameter) with no numeric argument causes the function to iterate until convergence or until the original value is seen again.

the numeric argument tells it to run a specific number of times.

https://code.kx.com/v2/ref/over/

there is no need to do this with a variadic function because the length of the ‘y’, ‘z’, etc parameters control the number of iterations

Hi Nick, thank you for your answer, I’m a newbie in q world, and tried to find out the answer for a long time.

thanks!
On Friday, August 16, 2019 at 12:26:52 PM UTC+9, Nick wrote:

using ‘/’(over) or ''(scan) with a monadic function (only including an ‘x’ parameter) with no numeric argument causes the function to iterate until convergence or until the original value is seen again.

the numeric argument tells it to run a specific number of times.

https://code.kx.com/v2/ref/over/

there is no need to do this with a variadic function because the length of the ‘y’, ‘z’, etc parameters control the number of iterations