# higher-order function with recursive

**URL:** https://forum.kx.com/t/higher-order-function-with-recursive/12129
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [April 20, 2021, 6:04pm UTC](https://forum.kx.com/t/higher-order-function-with-recursive/12129 "2021-04-20T18:04:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![srevurikdb1](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@srevurikdb1](https://forum.kx.com/u/srevurikdb1)
#### Post date: [April 20, 2021, 6:04pm UTC](https://forum.kx.com/t/higher-order-function-with-recursive/12129/1 "2021-04-20T18:04:00Z")

</div>

Hi All,  
I am new to KDB+ and q. I am trying implement&nbsp;higher-order function with recursive and found below code in the docs.  
I could not understand how it is working.  
Could you please explain below code.  
**code**

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

**results**  
 1 1 2 3 5 8 13 21 34 55 89 144

---

<div class="post-metadata">

### Author: ![ajay1](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@ajay1](https://forum.kx.com/u/ajay1)
#### Post date: [April 20, 2021, 6:26pm UTC](https://forum.kx.com/t/higher-order-function-with-recursive/12129/2 "2021-04-20T18:26:00Z")

</div>

This is generating Fibonacci sequence, in each iteration appending the sum of last two elements.

Can also use scan

q)last flip 10{x[1],sum x}\1 1

1 2 3 5 8 13 21 34 55 89 144

---

<div class="post-metadata">

### Author: ![unterrainer\_ale1](https://avatars.discourse-cdn.com/v4/letter/u/9dc877/32.png) [@unterrainer\_ale1](https://forum.kx.com/u/unterrainer_ale1)
#### Post date: [April 20, 2021, 7:22pm UTC](https://forum.kx.com/t/higher-order-function-with-recursive/12129/3 "2021-04-20T19:22:00Z")

</div>

Hi,  
so basically you are looking at one of the applications of iterators &nbsp;(previously known as adverbs). This particular form can be compared to a “Do” loop in other programming languages. Let’s have a look at the code, for simplicity let’s define the function f as following&nbsp;

f:{x,sum -2#x}

Then your code becomes&nbsp;

10 f/ 1 1&nbsp;

On a side note this could also be written as f/[10;1 1] which might make it easier to understand. In this case / (over) acts as an accumulator, executing your function f 10 times using 1 1 as initial parameter. the result of the first iteration then becomes the input of the second iteration and so on.&nbsp;

what does f do?&nbsp;

q is executed left of right, means from right to left. -2#x takes the last two elements of x (1 1), sum sums them and the result gets concatenated to the initial&nbsp;input x, thus after your first iteration you get 1 1 2. This will now become the input to your second iteration. A helpful way to understand over better is to use scan \ instead of over. Scan does basically the same as over except it outputs the intermediate steps. The result of over is the last output of scan.

q)10 {x,sum -2#x}\1 1

1 1

1 1 2

1 1 2 3

1 1 2 3 5

1 1 2 3 5 8

1 1 2 3 5 8 13

1 1 2 3 5 8 13 21

1 1 2 3 5 8 13 21 34

1 1 2 3 5 8 13 21 34 55

1 1 2 3 5 8 13 21 34 55 89

1 1 2 3 5 8 13 21 34 55 89 144

Hope this helps.&nbsp;

More details about Iteration can be found here https://code.kx.com/q/basics/iteration/

There is also a great white paper about it here&nbsp;https://code.kx.com/q/wp/iterators/

And the particular DO case is explained here:&nbsp;https://code.kx.com/q/ref/accumulators/

---

<div class="post-metadata">

### Author: ![srevurikdb1](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@srevurikdb1](https://forum.kx.com/u/srevurikdb1)
#### Post date: [April 21, 2021, 6:35am UTC](https://forum.kx.com/t/higher-order-function-with-recursive/12129/4 "2021-04-21T06:35:00Z")

</div>

Thanks&nbsp;Alexander for your **&nbsp;** explanation **.**
