<?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 Anu Soni on Medium]]></title>
        <description><![CDATA[Stories by Anu Soni on Medium]]></description>
        <link>https://medium.com/@anusoni25.2006?source=rss-2df3d4a73c6c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j8TX4oKZstVKZwO-uNjo_Q.jpeg</url>
            <title>Stories by Anu Soni on Medium</title>
            <link>https://medium.com/@anusoni25.2006?source=rss-2df3d4a73c6c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:56:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anusoni25.2006/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[Understanding foldl vs foldr Through One Simple Example]]></title>
            <link>https://medium.com/@anusoni25.2006/understanding-foldl-vs-foldr-through-one-simple-example-014c3780a1d6?source=rss-2df3d4a73c6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/014c3780a1d6</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[recursion]]></category>
            <dc:creator><![CDATA[Anu Soni]]></dc:creator>
            <pubDate>Sun, 19 Jul 2026 18:47:16 GMT</pubDate>
            <atom:updated>2026-07-19T18:47:16.718Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding foldl vs foldr</h3><p>I was doing one of my MeTTa assignment, my mentor asked me a simple question:</p><p>What is the difference in foldl and foldr, and when would you use over the other ?</p><p>I knew them by definitions only, but i couldn’t explain them clearly. Biggest drawback if you are in tech.</p><p>Here the explanation that finally made the idea click for me…</p><p>So, the problem :</p><p>Suppose we have a list : [1 , 2 , 3 ] and we want to add all the numbers.</p><h3>foldl (left fold):</h3><h3>foldl processes the list from left to right.</h3><p>foldl (+) 0 [1 , 2 , 3]</p><p>= ((0 + 1) + 2 ) + 3</p><p>= 6</p><p>Steps are like :-</p><ul><li>0 + 1 = 1</li><li>1 + 2 = 3</li><li>3 + 3 = 6</li></ul><p>Imp. ques: Why it is useful ?</p><p>The accumulator is updated before the recursive call, so it becomes tail-recursive. That means it is usually more to memory efficient for the LARGE LISTS.</p><h3>foldr (right fold):</h3><h3>foldr processes the list fromm right to left.</h3><p>foldr (+) 0 [1,2,3]</p><p>= 1 + (2 + (3 + 0))</p><p>= 6</p><p>Here, first the recursive call happens and function is applied while the call stack unwinds.</p><p>So, the <strong>key difference</strong> : Both gives the same result in case of addtion , and they are non-associative in case of substration. Let’s try by own.</p><p>As incase of substraction, different order → different result.</p><p>So, what i learned:</p><ul><li>foldl = left to right, tail-recursive, usually faster.</li><li>foldr = right to left, not tail-recursive.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=014c3780a1d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>