<story><title>Mindset shifts for functional programming (with Clojure)</title><url>https://blog.janetacarr.com/mindset-shifts-for-functional-programming-with-clojure/</url></story><parent_chain><item><author>roenxi</author><text>&amp;gt; Recursion over Looping&lt;p&gt;Part of what makes Clojure a great programming language is that you don&amp;#x27;t have to believe this if you don&amp;#x27;t want to. Nobody has yet convinced me that recursion has any sustained advantage over looping.&lt;p&gt;Using a loop is generally bad practice if a more specialised operation is available (don&amp;#x27;t loop if something is a simple map or reduce for example). But if the situation justifies a recursion then it usually justifies a loop unless the recursion is particularly neat. Recursion has the same problem as looping - it doesn&amp;#x27;t tell anyone anything about what the code is really doing. If I see map then I have implicit and explicit expectations about what is about to happen.&lt;p&gt;I enjoyed the article though.</text></item></parent_chain><comment><author>js8</author><text>I mostly agree, and proponents of FP shouldn&amp;#x27;t stress the recursion too much.&lt;p&gt;When I program in Haskell, I rarely use recursion myself. Most loops are just maps and folds. Need to build another structure from existing structure? Foldl&amp;#x27;. Once I understood that the value being folded can be arbitrarily complex, FP became simpler (but terser) than iterative programming for me.</text></comment>
<story><title>Mindset shifts for functional programming (with Clojure)</title><url>https://blog.janetacarr.com/mindset-shifts-for-functional-programming-with-clojure/</url></story><parent_chain><item><author>roenxi</author><text>&amp;gt; Recursion over Looping&lt;p&gt;Part of what makes Clojure a great programming language is that you don&amp;#x27;t have to believe this if you don&amp;#x27;t want to. Nobody has yet convinced me that recursion has any sustained advantage over looping.&lt;p&gt;Using a loop is generally bad practice if a more specialised operation is available (don&amp;#x27;t loop if something is a simple map or reduce for example). But if the situation justifies a recursion then it usually justifies a loop unless the recursion is particularly neat. Recursion has the same problem as looping - it doesn&amp;#x27;t tell anyone anything about what the code is really doing. If I see map then I have implicit and explicit expectations about what is about to happen.&lt;p&gt;I enjoyed the article though.</text></item></parent_chain><comment><author>PartiallyTyped</author><text>Maybe it&amp;#x27;s my background but recursion feels more natural, either you are at the base, or at a point with N-1 items below. This gives you two different invariants that you can use to simplify the code.&lt;p&gt;Small example, though not exactly comparable to looping.&lt;p&gt;I needed to create a human readable qualified path appended by a hash (to avoid conflicts). This qualified path was needed ahead of the construction of an object, and the logical name must have been used in both the hash and qualified path as it was global (Yes, it was an S3 bucket).&lt;p&gt;I wrote a function&lt;p&gt;&lt;pre&gt;&lt;code&gt;     uniqueQualifiedName: Construct x Optional(logicalId) x Properties -&amp;gt; string.
&lt;/code&gt;&lt;/pre&gt;
The recursive step is at the top, where the logicalId is transformed to a construct in the scope of the first argument, and the function is called again with NewConstruct x Nothing x Properties.  This could easily be adjusted to work with a list of logicalIds instead.&lt;p&gt;The invariant in the recursive step is that once the call returns, the construct at the end of the chain will be exactly the construct I inserted, and I can pop it safely, and the scope object will be none the wiser.&lt;p&gt;The invariant in the base is simply that I don&amp;#x27;t need to worry about logicalId being anything.&lt;p&gt;You can see then how easy it is to adapt this to work with a list, whereas with a loop you&amp;#x27;d have to write it from zero, and then roll back. Too messy imho.</text></comment>