• ### How to create recursive functions in patches?

Help me please! I need to implement the following code written in C # in the patch:

``````public static void FareiSequence(int n)
{
Console.WriteLine("{0} / {1}", 0, 1);
FareiSequence(n, 0, 1, 1, 1);
Console.WriteLine("{0} / {1}", 1, 1);
}

private static void FareiSequence(int n, int x, int y, int z, int t)
{
int a = x + z, b = y + t;
if (b <= n)
{
FareiSequence(n, x, y, a, b);
Console.WriteLine("{0} / {1}", a, b);
FareiSequence(n, a, b, z, t);
}
}
``````

How to organize recursion in a patch?

• Posts 3 | Views 1848
• @Alexandr-Tarasov You can process lists with [expr] or separate math functions but......
Recursion will cause a stack overflow in Pd so you will need to limit the depth using [until].
Explanation here...... https://forum.pdpatchrepo.info/topic/10805/struct-pointer-object-limitation
David.

• @Alexandr-Tarasov You can use recursion by creating loops within the data flow. As @whale-av mentioned, you have to be careful not to get into a stack overflow.

This simple example uses recursion to calculate the factorial of a given number. I used [moses 330] to prevent stack overflow, although the upper limit of numbers of Pd is reached much earlier: fact.pd

Each number is multiplied by the previous number and stored within the [* 1]. Then it is diminished by one and thrown into the loop again.

Posts 3 | Views 1848
Internal error.

Oops! Looks like something went wrong!