<?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 Sumant Bhaskaruni on Medium]]></title>
        <description><![CDATA[Stories by Sumant Bhaskaruni on Medium]]></description>
        <link>https://medium.com/@bsumantb?source=rss-1668e6d1efb1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Sumant Bhaskaruni on Medium</title>
            <link>https://medium.com/@bsumantb?source=rss-1668e6d1efb1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 11:22:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bsumantb/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[Thinking in code]]></title>
            <link>https://medium.com/@bsumantb/thinking-in-code-8a67dabe7c33?source=rss-1668e6d1efb1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a67dabe7c33</guid>
            <category><![CDATA[problem-solving]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[haskell]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sumant Bhaskaruni]]></dc:creator>
            <pubDate>Thu, 26 Aug 2021 10:59:51 GMT</pubDate>
            <atom:updated>2021-08-26T14:21:22.236Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*axtuxXXL8LtJOEJ6dvGafw.jpeg" /></figure><p>I recently found a programming challenge that asked me to write code that will determine the number of A0 papers you can make based on a given list of quantities of A0, A1, A2, etc. papers that you already have. The idea the problem is based on is simple, two A(n + 1) papers can be merged into one A(n) paper.</p><p>This is rather trivial to solve. If you are given a list that goes [1, 1, 2], that means you have 1 A0 paper, 1 A1 paper, and 2 A2 papers.</p><p>If you take the two A2 papers, you can merge them into one A1 paper. Which means you now have two A1 papers, which, you guessed it, can be merged into an A0 paper. To finish, you already had one A0 paper, so now you have two.</p><p>Assuming you haven’t already figured it out, you can simply solve this programmatically by:</p><ul><li>starting at the right (or the smallest size),</li><li>halving the number with integer division (or merging each pair into one),</li><li>adding it to the number to the left (or classifying your Frankenstein’d pair of smaller papers as a bigger one),</li><li>repeat until you have only one number left</li></ul><p>If you did everything right, you should now have the number of A0 papers you could&#39;ve made with the papers you had previously.</p><p>This sounded like a concept that I had learned of when I learned the language Haskell on a whimsy. Here’s how I implemented it:</p><pre>mergePapers a = foldr (\cur acc -&gt; (acc `div` 2) + cur) 0 a</pre><p>I’ll admit, this probably reads like Greek to anybody that hasn’t used Haskell, even to those who’ve programmed in any other language to any extent. But, in order to give those people a frame of reference, here’s how I did it in Java:</p><pre>public int mergePapers(int[] a) {<br>    int acc = 0;</pre><pre>    for (int cur = a.length - 1; cur &gt;= 0; cur--) {<br>        acc = acc / 2 + a[cur];<br>    }</pre><pre>    return acc;<br>}</pre><p>I promise the Haskell code isn’t particularly obfuscated, it is reasonably close to idiomatic code. It does also functionally do the same thing as the Java code does and the steps that I had described earlier.</p><p>I can’t teach you Haskell, but what I can do is try and rewrite the code with a style similar to what most programmers are familiar with, which is, of course, C-style syntax:</p><pre>mergePapers(a) = foldr(<br>    ((cur, acc) -&gt; div(acc, 2) + cur),<br>    0,<br>    a<br>)</pre><p>The only really notable thing I changed was how functions are denoted. In Haskell, both defining and calling a function looks like func arg1 arg2, whereas it would look like func(arg1, arg2) in most other languages. The other thing I changed was what the lambda (or anonymous function) looked like, which of course varies a lot from language to language but I&#39;ve simply used (arg1, arg2) -&gt; arg1 + arg2, which is just a function that takes two arguments and returns their sum.</p><p>One other trivial thing to get out the way is the div function, which is simply an integer division function.</p><p>Now, onto the actual meat of the code, the foldr function. Its purpose is to <em>&quot;fold a function over a list from the right with an initial accumulator&quot;</em>. If you understand what this means, you&#39;ll realize that it is doing basically the entirety of my work for me. If you don&#39;t, well, the best way to explain it is to liken it to the Java code.</p><p>Like I said earlier, both bits of code are functionally doing the same thing:</p><ul><li><em>with an initial accumulator</em></li></ul><p>Let’s start with likely the most obvious part. In Java, I’ve used an accumulator by defining int acc = 0. In Haskell, this is simply the second argument 0 I have provided the foldr function.</p><ul><li><em>over a list from the right</em></li></ul><p>In order to do this, in Java, I created a for loop that starts at the end of the array a and ends at the beginning with for (int cur = a.length - 1; cur &gt;= 0; cur--). In Haskell, this is done by supplying the list as the third argument to the foldr function as opposed to, you guessed it, the foldl function.</p><ul><li><em>fold a function</em></li></ul><p>To tie it all up, in Java, I add to the accumulator over time with however many paper pairs I’m merging using acc = acc / 2 + a[cur]. In Haskell, this is done by supplying the function (cur, acc) -&gt; div(acc, 2) + cur as the first argument, which does the same thing as the Java snippet, just as a function instead.</p><p>Now, you may say I’ve written all this to say Haskell has a convenient function that Java doesn’t. You would be right… to a certain extent. But what struck me more while writing this code is how naturally it translated from my thought process and how satisfied I was after having written this trivial bit of code. As I broke down the problem, I found myself <em>thinking</em> in Haskell, as if it were another spoken language.</p><p>This isn’t to say people can’t experience this in other languages. Is it a stretch to say it for Java? Probably. (I definitely do not have a passionate hatred for Java.) Python, for example, is a language I would describe as closest to being a spoken language with a lot of syntactical constructs looking like everyday English.</p><p>Haskell however, isn’t exactly known for that. It is a functional language, which makes it look rather mathematical in nature. Writing Haskell code feels like composing the right functions in the right order to get the desired result. But, at that moment, while solving this simple problem, I found that Haskell mirrored my thoughts almost perfectly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a67dabe7c33" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>