<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Malo on Medium]]></title>
        <description><![CDATA[Stories by Daniel Malo on Medium]]></description>
        <link>https://medium.com/@danielmaloosorio?source=rss-2a73391b8557------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*d_53EmkNcHxZjre3</url>
            <title>Stories by Daniel Malo on Medium</title>
            <link>https://medium.com/@danielmaloosorio?source=rss-2a73391b8557------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 23:54:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@danielmaloosorio/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Recursion or Loops?]]></title>
            <link>https://medium.com/@danielmaloosorio/recursion-or-loops-f6128fd87469?source=rss-2a73391b8557------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6128fd87469</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[loops-in-python]]></category>
            <category><![CDATA[church-turing-hypothesis]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Daniel Malo]]></dc:creator>
            <pubDate>Sat, 11 Jun 2022 05:01:56 GMT</pubDate>
            <atom:updated>2022-06-13T14:52:46.621Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Any recursive algorithm can be translated into a loop, be it a for-loop or a while-loop.</em></p><p><em>Have you wondered if the last problem you solved with recursion is actually safer to translate into a loop? Give it a try!</em></p><p><em>If you didn’t get very far with your own algorithm, don’t worry! I can show you how to do that in a few simple steps:</em></p><ol><li><em>Put your code into a function.</em></li><li><em>Initialize an empty cache variable, preferably an iterable.</em></li><li><em>Initialize a global iterator variable that starts at 0 (or </em><strong><em>n</em></strong><em> for countdown algorithms).</em></li><li><em>Start a while loop.</em></li><li><em>Write an escape condition to be the base case (depending on the language, it may be at the while statement).</em></li><li><em>Replace the rest of the logic with operations applied to your cached variable. It’s important that if you work with parent objects, you push their pointers onto your cache to keep track of those.</em></li><li><em>Keep track of iterations with your ‘global’ iterator. If needed make an escape condition with this variable.</em></li><li><em>Return your cache variable if the function is a pure function.</em></li></ol><p><em>The best thing about this is that this process is completely reversible! It’s a two-way street when it comes to loops and recursion. The following are a general translation of loop to recursion:</em></p><ol><li><em>Put your code into a function (crucial step).</em></li><li><em>Set your parameters (and iterator/escape variables) to a default value in the definition of the function parameters.</em></li><li><em>Make scenarios for the base case(s), and return a base value if your function expects a return value.</em></li><li><em>For the rest of the possible scenarios, make conditions to enter said scenarios.</em></li><li><em>Inside each case write the logic that represents your algorithm.</em></li><li><em>And still, inside each case, return a call to the function (the name needs to be the same as in the definition). Overwrite the default inputs with your changing variables and iterator/escape variables.</em></li><li><em>If the return value requires an operation (i.e. an arithmetic operation), you can include it in the same return statement. (You don’t always have to only return the called function.)</em></li></ol><p><em>Here is the Mandelbrot function in both recursive and loop form in Python 3.9:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/0*jrWz-T9kQuZgs8Id" /><figcaption>Recursive Function That Calculates Values Of The Mandelbrot Set</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*4wk7lBQyOECVU_4MvHpcBw.png" /><figcaption>Looping Function That Calculates Values Of The Mandelbrot Set</figcaption></figure><p><em>When run, both codes print out ‘(-1.78178654827319+0j)’. As of this version of Python, the maximum default recursion depth is 1000 (which can be modified), hence </em><strong><em>n</em></strong><em> defaults to 997. And as for the looping function, I used two variables instead of an array because it saves computer resources and we only need the last cached value to calculate the next.</em></p><p><em>So we walked through how to translate algorithms from recursive to loop form and backward, this is in fact a mathematically proven statement in the </em><a href="https://mathworld.wolfram.com/Church-TuringThesis.html"><em>Church-Turing Thesis</em></a><em>, that all recursive algorithms can be translated into loops and vice-versa. In theory, though not always feasible in practice. In short, under the hood, recursion is just a loop that pushes to a stack and pops results back up the stack once it’s done.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6128fd87469" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>