<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Recursive count up&#x2F;down]]></title><description><![CDATA[<p>Hello, my first ever post on any forum on the internet! I have a question about recursive counts which I've seen named as dimensional counts. I wondered if anyone could help me achieve this in PD using two variables, the count value and the dimension value. Examples are a 2 dimensional countdown from 4 would be 4321 a 3 dimensional countdown from 4 would be 4321 321 21 1 a 4 dimensional countdown from 4 would be 4321 321 21 1 321 21 1 21 1 1. Can any of you clever people help please? Been scratching my head about this for a little too long now!  I've been trying to make one patch which deals with any two variables. Thank you in advance.</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down</link><generator>RSS for Node</generator><lastBuildDate>Sat, 13 Jun 2026 15:40:18 GMT</lastBuildDate><atom:link href="http://forum.pdpatchrepo.info/topic/14556.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 20 Dec 2023 23:04:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Wed, 20 Dec 2023 23:04:39 GMT]]></title><description><![CDATA[<p>Hello, my first ever post on any forum on the internet! I have a question about recursive counts which I've seen named as dimensional counts. I wondered if anyone could help me achieve this in PD using two variables, the count value and the dimension value. Examples are a 2 dimensional countdown from 4 would be 4321 a 3 dimensional countdown from 4 would be 4321 321 21 1 a 4 dimensional countdown from 4 would be 4321 321 21 1 321 21 1 21 1 1. Can any of you clever people help please? Been scratching my head about this for a little too long now!  I've been trying to make one patch which deals with any two variables. Thank you in advance.</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down</guid><dc:creator><![CDATA[tildebrow]]></dc:creator><pubDate>Wed, 20 Dec 2023 23:04:39 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Thu, 21 Dec 2023 01:10:00 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/tildebrow">@tildebrow</a> To be honest, I've never heard of recursive counting before, so I can only conclude from your examples how it should work. This is my attempt:</p>
<p><a href="/uploads/files/1703120667486-recursive-count.pd">recursive-count.pd</a></p>
<p>The patch doesn't give any output for dimension 1, but I assume you won't need that anyway.</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/2</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/2</guid><dc:creator><![CDATA[manuels]]></dc:creator><pubDate>Thu, 21 Dec 2023 01:10:00 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Tue, 02 Jan 2024 03:23:21 GMT]]></title><description><![CDATA[<p>I gave it a shot too. I thought it would be more intuitive to do recursive stuff in a textual language so I figured it out in lua first:</p>
<pre><code>function DimensionCount(dimension, number)
    if(dimension == 1) then print(number)
    else
        DimensionCount(dimension - 1, number)
        number = number - 1
        if(number &gt; 0) then DimensionCount(dimension, number) end
    end
end
</code></pre>
<p>after that I tried to translate it into pd. The tricky part is storing the variables on the stack while a 'function call' happens. The only way I could figure out how to do it was lists in a <code>[trigger]</code><br />
edit: changed to <code>[t f]</code> for efficiency <a href="/uploads/files/1704165799084-dimensional_count.pd">dimensional_count.pd</a></p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/3</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/3</guid><dc:creator><![CDATA[seb-harmonik.ar]]></dc:creator><pubDate>Tue, 02 Jan 2024 03:23:21 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Thu, 21 Dec 2023 12:09:05 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/manuels">@manuels</a> Thank you so much! This is exactly the output I was looking for. Could you possibly give me any pointers on learning material for the thought process that would help me get to the point where I'd be able to solve something like this for myself? I'm so impressed and grateful!</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/4</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/4</guid><dc:creator><![CDATA[tildebrow]]></dc:creator><pubDate>Thu, 21 Dec 2023 12:09:05 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Thu, 21 Dec 2023 12:15:11 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/seb-harmonik-ar">@seb-harmonik.ar</a> Thank you for taking the time to do this! It's amazing to see two working versions, one from <a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/manuels">@manuels</a> and yours too. I'm now going to have fun going through to see how both versions work. The storing of variables was what was stumping me. Do you have any pointers on learning materials I could study to get better at thinking in a more logical way to help get me to the point of solving things like this? Thanks again - really appreciate your contribution and so impressed!</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/5</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/5</guid><dc:creator><![CDATA[tildebrow]]></dc:creator><pubDate>Thu, 21 Dec 2023 12:15:11 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Thu, 21 Dec 2023 12:52:00 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/tildebrow">@tildebrow</a> Don't use my poor attempt on recursive counting in practice, the solution of <a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/seb-harmonik-ar">@seb-harmonik.ar</a> is much better! I'm actually quite impressed by it ... That's probably as elegant and efficient as you can get with this in Pd.</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/6</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/6</guid><dc:creator><![CDATA[manuels]]></dc:creator><pubDate>Thu, 21 Dec 2023 12:52:00 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Thu, 21 Dec 2023 14:05:04 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/manuels">@manuels</a> thank you for your feedback. Still going to go through both with a fine tooth comb to try. to up my game!</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/7</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/7</guid><dc:creator><![CDATA[tildebrow]]></dc:creator><pubDate>Thu, 21 Dec 2023 14:05:04 GMT</pubDate></item><item><title><![CDATA[Reply to Recursive count up&#x2F;down on Fri, 22 Dec 2023 04:42:53 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-a" href="http://forum.pdpatchrepo.info/user/tildebrow">@tildebrow</a> well I have nothing specific, look up 'recursion'. What really helps is an exposure to functional programming.<br />
But can try to walk through my process for this solution in depth:<br />
first thing to do was look at the sequence and think about it.<br />
If we increase the dimension, any particular 'level' has to expand into the result of itself if it were the argument for the current dimension.</p>
<p>e.g. if the current dimension is 2 the sequence is 4321. When we increase the dimension the element '4' has to expand to 4321 (the result of dimension 2 count 4), the element '3' has to expand to '321' (the result of dimension 2 count 3) etc. If all these recursive expansions are done after one another then we will get the entire dimension 3.</p>
<p>So, if we want dimension 3 we have to call dimension 2 on each of the 'elements'. Each dimension 2 element has to call each of its dimension 1 elements (recursive). When 2 calls dimension 1 with a certain element it's over because those are our single values, therefore dimension 1 with a certain number is our 'base case' and we just print/output the number.</p>
<p>When implementing recursion we often start with a 'base case'. that's why dimension 1 is the 1st check in the recursive function.</p>
<p>My first implementation actually used a counter internally:</p>
<pre><code>function DimensionCount(dimension, number)
    if(dimension == 1) then print(number)
    else
        for i = number, 1, -1 do
            DimensionCount(dimension - 1, i)
        end
    end
end
</code></pre>
<p>but, you can also count down recursively:</p>
<pre><code>function RecursiveCount(currentCount)
    print(currentCount)
    if(currentCount &gt; 1) then RecursiveCount(currentCount - 1) end
end
</code></pre>
<p>I thought there must be a way to combine these, so the next version was</p>
<pre><code>function RecursiveCount(dimension, currentCount)
    DimensionCount(dimension - 1, currentCount)
    if(currentCount &gt; 1) then RecursiveCount(dimension, currentCount - 1) end
end

function DimensionCount(dimension, number)
    if(dimension == 1) then print(number)
    else
        RecursiveCount(dimension, number)
    end
end
</code></pre>
<p>from there the last piece was actually combining them into 1 function:</p>
<pre><code>function DimensionCount(dimension, number)
    if(dimension == 1) then print(number)
    else
        DimensionCount(dimension - 1, number)
        if(number &gt; 1) then DimensionCount(dimension, number - 1) end
    end
end
</code></pre>
<p>then cleaning it to make it more <code>[spigot]</code>/pd friendly.</p>
<p>Like I said the challenge in converting it to a pd patch is mainly keeping values on the stack. For this part you have to understand a bit about how pd calls its outlet code and look up stuff about the 'call stack'. The call stack is also a fundamental concept in recursion.<br />
I considered <code>[swap]</code> and <code>[trigger]</code> to hold values on the stack, but I'm not even sure swap would work and trigger can't output multiple different atoms. Therefore I stored the stack variables in a list, and using <code>[trigger]</code> passed one version into the first 'call' to DimensionCount and the other version to the 2nd (if number &gt; 0 at the spigots)<br />
the 2nd list is saved on the stack since it is in the middle of a C function call. (in pd's outlet code for <code>[trigger]</code>)<br />
Just now I tried making a version based on the original explicit counter-down version, but since 'i' has to be restored after every recursion it ends up being equivalent I think.</p>
<p>Also, know that if you need to you can manage a stack explicitly. In this case, you would use one or two arrays to store the values of 'dimension' and 'number'. Everytime you want to make a function 'call' you have to store the previous values in the arrays and increment the size, then when the function call is over you decrement the arrays and get the old values back out. That's basically how you convert recursive calls into iterations/loops, and the alternative to recursion to manage variables in recursive problems.</p>
]]></description><link>http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/8</link><guid isPermaLink="true">http://forum.pdpatchrepo.info/topic/14556/recursive-count-up-down/8</guid><dc:creator><![CDATA[seb-harmonik.ar]]></dc:creator><pubDate>Fri, 22 Dec 2023 04:42:53 GMT</pubDate></item></channel></rss>