When Haskell is the right choice, and when it is not

A language is a tool with a shape, and the shape decides what it is good for. Haskell is shaped by the idea that a program is a value and that the type checker should know what the program does before it runs. C++ is shaped by the idea that a program is a machine and that the programmer should be able to say exactly what the machine does, including the parts that are unsafe by design.

The pages before this one put the two shapes next to each other with real programs and real numbers. This page is the summary, and it tries to be fair in both directions: there are programs that Haskell should not be used for, and there are arguments for Haskell that do not survive a budget.

What each language is for

Haskell is a language for logic: a compiler, a pricing engine, a validator, a protocol implementation, a scheduler. In those programs the difficulty is the rules, the rules change, and a mistake is expensive. A type checker that refuses a missing case, a parser combinator that makes the grammar readable and a monad that keeps the effects in an order are all the same benefit: the program says what it means.

C++ is a language for machinery: an operating system, a database engine, a game engine, a driver, a codec, a library that other programs link against. In those programs the difficulty is the machine, the memory and the boundary, and the language has to let the programmer control all three, including the parts that no safe language would allow.

When Haskell is the right choice

The rules are the program. If the hard part is a set of business or protocol rules that change, a closed sum type and an exhaustive match pay for themselves the first time a rule is added: the compiler lists every place that has to decide. The page on one change made in both shows the price on the other side, which is real.

Concurrency is in the design. STM is the reason to reach for Haskell in a service with shared state: a transaction composed of several variables is correct by construction, and the measured page shows what it costs against a mutex. If a program has one counter, a mutex is faster. If it has a rule about two of them, STM is the cheaper code to keep correct.

The data is a pipeline. Sums, folds, maps and filters over a stream, written as functions and tested as functions, are what Haskell is shortest at. The laziness that makes an infinite list work is the same property that makes a pipeline read like a specification.

The team is small and stays. The type checker is a reviewer that never sleeps, and a small team with a long-lived codebase gets the most from it. A team of two hundred with a high turnover pays the learning curve on every hire.

When C++ is the right choice

The code already exists. A C++ codebase, a C++ team and a C++ ABI are three assets that no rewrite keeps. The honest comparison is rarely between two languages on a blank page; it is between a new module in the language of the codebase and a rewrite in another one.

The machine is the constraint. Latency, cache behaviour, allocation patterns, the number of instructions in a loop: the throughput page measured a factor of 1.5 on a loop of bit operations and a factor of eight on one with a division. The eight was a compiler difference and the 1.5 is what the two languages look like when the work is the same; if a program is bound by that work, the 1.5 is the number that matters.

Something else has to link against it. A shared library with a C ABI, a plugin interface, a driver, a runtime for another language: C++ is the lingua franca of the boundary, and a Haskell library that has to be called from five languages becomes a C++-shaped problem anyway.

Memory is the product. A device with 64 kilobytes of RAM, a long-running process with a hard limit, a program whose peak has to be known: the memory page measured a Haskell list at twenty-five megabytes where the C++ vector used eight, and the runtime of the lazy version at two megabytes of resident memory. Both numbers are honest and both are the wrong shape for a device.

What each one costs

Haskell C++
a new developer is productive in weeks to months, and the type system is the steep part months, and the lifetime rules are the steep part
libraries a smaller ecosystem, and a package set is needed to keep it coherent a library for everything, and a build system to hold them
the runtime a garbage collector, tunable, with a floor of a few megabytes no runtime, and a program that can be smaller than its own output
the class of bug the compiler stops a missing case, a wrong type, an ignored error a wrong type, a missing override, an unused result
the class of bug it does not a space leak, a partial function, a lazy accumulator a lifetime, a race, an out-of-bounds write, an overflow
the tools before shipping the compiler, a linter, a formatter the compiler, an analyser, a formatter, a sanitizer, valgrind

The last two rows are the trade in one line. Haskell moves the checking to the compiler and leaves a smaller set of run-time traps; C++ moves the checking to a pile of tools and leaves a larger set of traps that the tools do not always catch. The first rows are why a small team with a clear problem is happier in Haskell and a large team with an inherited codebase is happier in C++.

Using both

The choice is not always a choice. The most common shape in a real product is a C++ core that has to be fast or has to link against something, and a layer above it where the rules live: the API, the validation, the pricing, the schedule. That layer is where Haskell fits without asking the core to move, and the boundary between the two is a C ABI that neither language owns.

The course lesson on calling C from Haskell shows the C side of that boundary from the Haskell end, and being called from C shows the other direction, which is the one a mixed product needs. The price of the boundary is the subject of those lessons: the two sides do not share a type system, so every value that crosses it is checked by hand and every error becomes a return code.

The opposite arrangement is rarer and worth naming: a Haskell program that shells out to a C++ tool for one job, or loads a C library through the FFI for one function. Both are ordinary and both are cheaper than rewriting the other side.

A checklist

Six questions, in the order that decides most real cases:

  1. Is there a codebase? If yes, the language is chosen and the question is which module to write in which style.
  2. Is the hard part the rules or the machine? Rules point to Haskell, the machine to C++.
  3. Does something else have to link against it? Then the boundary is C, and the language behind the boundary is free.
  4. Is there a memory or latency budget? Measure it before deciding; the pages here give the method and the numbers for one machine.
  5. Who will maintain it in three years? The answer decides more than the benchmark.
  6. Can the team hire for it? A language nobody on the team knows is a training project before it is a product.

None of those questions mentions syntax, and that is the closing point. The difference between the two languages that matters is not how a loop is written; it is what each compiler refuses to accept, and what each runtime costs. The pages of this set show both with programs that were built and run, and the numbers are from one machine, which is the only honest way to have numbers at all.

This is the last page of the set. It starts again at Haskell and C++ side by side, and the Haskell course it belongs to begins at what Haskell is.

The numbers quoted on this page were measured in this container with g++ 15.2.0, GHC 9.10.3 and the tools of the image on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image