by greener_grass · 2y ago · 0 comments
Yes! This is the real win of functional over imperative languages. It means you can understand blocks of code in isolation and then compose them into larger blocks. Bottom-up programming. This is what is missing from the imperative paradigm, and why that approach struggles to scale with problem complexity.
Nevertheless, I'd take a functional language over procedural any day. The horror of mutable memeber variables changing all over their class is real.
1. The right-hand-side of the declaration
2. Where you are in program execution
Consider this JavaScript:
let x = 1;
x += 1;
x *= 3;
What is the value of x?Valid answers are 1, 2 and 6.
Now lets restrict our JavaScript to pure expressions:
let x1 = 1;
let x2 = x1 + 1;
let x3 = x2 * 3;
The values of x1, x2, x3 are full defined by the RHS; they do not change once bound.This makes it easier to reason about the code - you only need to follow the definitions.
This is an unnatural way to write JavaScript, but some languages make this easy (Clojure, Elm, OCaml, F#, Haskell, Erlang, ...).