3 ms·
In most languages, the value of an expression depends on two things: 1. The right-hand-side of the declaration 2. Where you are in program execution Consider
by greener_grass 2y ago
In most languages, the value of an expression depends on two things:
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, ...).