Recursion in Anemone

Greetings -

I’m trying to do what I thought would be a simple recursion problem with Anemone and am failing pretty spectacularly.

I am creating a string of rectangles who’s Length and Offset are chosen randomly from a list of values. Each box is offset from the previous:
Y(n) = Y(n-1)+Length(n-1)+Offset(n)

I thought that Anemone would let me solve the recursion problem of having to add an offset list to itself, but I’m not finding a solution that doesn’t throw an error. Am I just going about this the wrong way? Any help would be greatly appreciated.

I think you don’t understand how anemone works (or gh wires?). In your second screenshot you have D0 output plugged into D0 input, this is not allowed. In last screenshot you have L output into B input, this is not allowed. The result of D0 loop end already goes to the output D0 loop start in each iteration. Anyway no file, so can’t help further.

Eyah, you’re definitely seeing the signs of frustration and desperation here… I swear I’ve made working patches before.

I’ve attached the test file I’m using just to try to get this number sequence generated. No geometry yet, but the desired sequence is written out
Thanks (13.5 KB)

Hi @paul13,

The solution to your problem is pretty straightforward, but Michael is right, you don’t seem to fully grasp what loops can accomplish yet.
In fact, you don’t need to provide all the data beforehand! You simply calculate it iteratively inside the loop, starting at a single or multiple values. At each iteration the loop, performs its task(s) on the result of the previous iteration, if recursion is desired.

For your example, you simply provide the first value, Y(n-1), to the D0 input of the Loop Start, which is 0. Inside the loop, you need to get offset(n) and lenght(n-1) at each iteration.
lenght(n-1) is quite straightforward, since it’s simply the current iteration count C, incremented by 1 (since the loop count starts at 0).
offset(n) can be provided by a series of even numbers that starts at 0. At each iteration of the loop, you simply get the corresponding even offset with the current iteration count, since the series has a length equal to the number of loop iterations. You don’t have to provide the series to a loop input, since it is constant, meaning that it isn’t iteratively changed by the loop.
All that remains is to add all the values together and forward their sum to the D0 input of the Loop End component. This value, Y(n-1), will get looped and changed at each iteration. Simply set the Loop End component to Record Data, which means that it records D0 at each iteration. (10.6 KB)


Thank you! Yes, I am still wrapping my head around loops in Grasshopper, and this a helpful starting point. Trying to move the whole list through the loop was where I was getting caught up.
In my final version, I’m going to be choosing The lengths and offsets randomly from a list of values, which I think I understand now will happen within the loop.
thanks again - I really appreciate it.

1 Like

Still a work in-progress, but is starting to do what I want it to, now I just have to figure out the best way to do multiple columns (possibly nesting this loop in another one).
thanks again @p1r4t3b0y !! (17.8 KB)

I have no idea if nested loops are possible! Do you know that by any chance, @Michael_Pryor?

Why do you thin that you need a nested loop? What are you trying to achieve?

Yes, nested Anemone loops are possible. Folks, please don’t confuse iteration (loops) with recursion as they are not the same thing.

1 Like

To understand recursion you first need to understand recursion.


I was hoping that link

would take me back to the start of your post :smile:

1 Like

Indeed they are and there are examples of them in anemones example files.

Obligatory XKCD



@p1r4t3b0y - I think I’ll nest loops to make multiple columns, since for each column, I’ll need Y position to go back to zero. An outer loop that loops once per column, and the inner loop which handles the elements in each column.