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.”
Why Functional Languages?
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.
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.
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”!
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.
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”.