Ask HN: When is pure functional programming beneficial?
I work full-time as a Haskell programmer, I stream myself building various Haskell projects once a week at <a href="https:&#x2F;&#x2F;twitch.tv&#x2F;agentultra" rel="nofollow noreferrer">https:&#x2F;&#x2F;twitch.tv&#x2F;agentultra</a>, I maintain a couple of Haskell libraries, and I occasionally do other things too.<p>I&#x27;ve been a professional programmer for a little more than twenty years and have been using Haskell to this degree for about the last 4 or so. A good chunk of my career was dedicated to Python and C. I&#x27;ve done work in C++ and Javascript. Dabbled in Common Lisp in my spare time. Which is to say, I&#x27;ve ran the gamut of functional, imperative, object-oriented, etc.<p>I got started in OCaml first by taking the Inria course. I later tried Haskell by taking the University of Glasgow course. I often thought of myself as preferring FP but I always worked in languages with escape hatches where I could retreat to familiar territory. It was easy to rationalize this as, &quot;using the right tool for the job.&quot; However thanks to some smart people in my life I decided to dip my toes in languages that were FP-first (OCaml) and later FP-only (Haskell).<p>&gt; Is there a threshold after which Haskell&#x27;s purely functional paradigm shines the most?<p>I think you just have to jump in the pool and go for it. There&#x27;s no threshold. You can write scripts, programs, whole systems in Haskell. There&#x27;s no point where, &quot;it becomes worth it.&quot;<p>I&#x27;m certain at this point that you can write a relational database engine that is comparable in performance and features as any other in the market in Haskell. You can write video games in it. You can write short helper scripts and tools. Word editors, websites, compilers, etc.<p>The threshold is how willing you are to leave behind what you&#x27;re already familiar with and learn a new way of programming that will make you feel like a complete beginner again for a while. Are you willing to re-learn programming over again? I don&#x27;t know why this is surprising to people but if you started with a C-like language and picked up other C-like languages along the way, picking up Haskell isn&#x27;t going to be as easy: there&#x27;s nothing familiar you can use in your C-like knowledge that will be useful for a long while.<p>I&#x27;ve been doing it for a while now and there are benefits to it which is why I still program in Haskell and got a job writing code in Haskell. You likely already have a good deal of the tools needed to reason about how to design programs in a pure FP language: sets and high-school level algebra. You have notions built into the type system in Haskell like equality, relations, and constraints. Once you start jogging your brain on how to think this way you can design most of your program in types. And when you do it this way, there tends to be only one or a couple of ways to implement a program that satisfies those types... it&#x27;s almost a mechanical process of filling in the holes.<p>From a practical perspective of a former Python programmer, I don&#x27;t have to write a good deal of unit tests I would usually start with when building a Python program. Armed with a well-thought out handful of types the compiler will reject any program that isn&#x27;t correct by construction which allows me to focus more of my efforts on things that are more important to me: is the logic correct, etc. I&#x27;m not testing, &quot;are these things equal,&quot; &quot;does this throw an expected error,&quot; &quot;is this actually a valid function,&quot; etc.<p>Another weird thing that happens when you learn to program in Haskell is that control flow is kind of... represented in data types. Since Haskell is expression-oriented and execution is carried out by evaluation we get neat features like whole program analysis of all code branches. If I add a new constructor to a sum type, I get instance feedback on all the places where I need to now handle that case. This is super useful.<p>However... it can be a challenge getting to this place where you&#x27;re comfortable with it. I had a hard time when I started a small Haskell project early on to make a simple web app. I was so used to logging being a single import away that having to learn all these new concepts in Haskell like, &quot;monad transformers,&quot; just to add logging to my app seemed like a bunch of busy work for something that was, &quot;solved,&quot; in my mind. The key to getting through moments like that is to forget your past experiences and just open your mind to being a beginner again.<p>It becomes useful later on when you realize that composition is so incredibly useful. You start to miss it when you go back to procedural languages and there are no guarantees and nothing composes.