Why Blockchain Engineers Should Learn Functional Programming

As a software engineer who wants to work with blockchains, you may ask: what languages should I learn?  Answers on the internet (e.g., here, here, and here) mostly recommend prominent languages like C++, Python, Java, and Javascript, and the smart contract language Solidity.  None of them mention any functional languages!  In this post I’ll explain why they should.

Blockchain is a technology with applications in many areas.  The most common uses of the technology include cryptocurrencies, banking/FinTech, and smart contracts.  These uses have one thing in common: mistakes are deadly.  They all involve financial transactions that are time sensitive and mistakes can be very costly.

This is where functional languages come in.

“Sometimes, the elegant implementation is a function.
Not a method. Not a class. Not a framework. Just a function.”

–John Carmack

Why Functional Languages?

Most of the widely used languages (including the above mentioned: C++, Java, and Javascript) are imperative.  In imperative programming, computations are structured as sequences of instructions that operate by making modifications to the state of the program.  Functional languages operate by declaring functions. The output value of a function depends only on the arguments that are passed to the function.  Because of this, functional languages have a few advantages that minimize mistakes:

Immutability

Unlike in imperative languages, in functional languages, variables are immutable, and does not depend on the state of the program.  Because in functional languages results only depend on the inputs, the results are more predictable.  This reduces errors and increases stability.

Type Safety

Modern functional languages are generally statically typed, which means that they type check (verify all functions have the correct input types passed to them) at compile-time as opposed to run-time.  This ensures that all program in production are type safe.  Even though some imperative languages are also statically typed, they don’t offer as many safety guarantees as modern functional languages.

Purity

In pure functional languages like Haskell, you can prove the absence of unwanted side-effects.  Because functional languages operates with functions that are type safe, you can control whether a function in the code has any interaction with the outside world more easily.

And Many more…

E.g., memory safety, ease of parallelization and distribution, etc.  See here and here  for more details.  In fact, functional languages OCaml and Haskell were rated here as the top two “perfect blockchain languages”!

Current Applications

There are blockchain protocols and their smart contracts that are already using functional languages:

  • Tezos is written in OCaml.  Although its official smart contract language is Michelson (stack-based), you can also use Liquidity, which is a fully typed functional language that uses the syntax of OCaml, and strictly complies with Michelson security restrictions.
  • Cardano is written in Haskell.  Its smart contract language is Plutus.
  • æternity is written in Erlang and its smart contract language  is  Sophia.
  • Coda is written in OCaml.

Other blockchain apps that are written in Haskell include Kadena and BlockApps

In conclusion, functional languages have advantages over imperative languages in blockchain.  They are already well-used in the blockchain world, and its popularity is increasing.  If you want to get into blockchain, you should consider learning functional programming!

Edit (Sep 2019): I consider Michelson not a functional language because it’s stack-based. I recently found out that there are disagreements regarding this. I’ve edited my wording describing Michelson as “not functional” to “stack-based”.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.