Haskell and C++ side by side

These pages are for programmers who write C++ and are learning Haskell, and for programmers who write Haskell and have to work with C++. Each one takes a single task, writes it in both languages, builds both programs with the real compilers and shows the two outputs next to each other. Then it explains what differs and why: what the compiler checks, what the runtime costs, and what the programmer has to remember.

Every program on these pages was compiled and run in the container described at the bottom of each page, and every number was measured there: five runs, a minimum and a median, the flags and the runtime settings written down. Where a tool could not run, the page says so instead of showing output it never got.

The pages are independent of the Haskell course: read one when its topic comes up in your work. The C++ side is the C++ course, and the Haskell side starts at algebraic data types.

ADT Algebraic data types and class hierarchies A shape type as a closed sum and as an open hierarchy, and the case each compiler fails to notice. Open Pure Immutability and pure functions An account copied and returned against an object changed in place, and the by-value copy that throws an update away. Open Lazy Laziness and eager evaluation Five squares out of a million: a vector, a C++ view and an infinite Haskell list, with the calls each one made. Open Class Type classes, templates and concepts One describe and one twice as a class with instances and as a template with a concept, and the bool that prints 1. Open Maybe Maybe, Either and exceptions A parse that can fail with optional, expected and try against Maybe and Either, and the three ways it ends badly. Open Text Strings, Text and ByteString against std::string Twelve bytes and ten characters, the case mapping only Text gets right, and the printf column that moves. Open STM STM against a mutex Four million increments four ways, the lost updates of a plain long, five runs of each and the medians. Open Speed Throughput: the same loop in both Twenty million iterations with a division and with a mask, four builds, five runs each, min and median. Open Memory Memory: +RTS -s against /usr/bin/time A million integers in a vector, a consumed list and a retained list, at 10, 2 and 64 megabytes. Open Change One change made in both A perimeter and a triangle added in each language, and what each compiler enforces when the change is half done. Open Start What a reader of each language needs first The first program, the toolchain, the concepts in the order each one needs them, and what a team needs to ship. Open Tools Tooling: ghc, cabal and hlint against g++ and the sanitizers clang-format, clang-tidy, cppcheck, the sanitizers and cmake, with the real reports and the one that will not start. Open Choose When Haskell is the right choice, and when it is not The closing page: what each language is for, what each costs, how to use both, and a checklist for the decision. Open

The C++ programs were built with g++ 15.2.0 (Alpine 15.2.0) and the Haskell programs with GHC 9.10.3, and both were run on Alpine Linux 3.24.3 (x86_64) in a Docker container with 4 CPUs.