by davidrupp · 5y ago · 0 comments
[1] https://clojuredocs.org/clojure.core/let#example-542692c7c02...
(let [a 10
a (+ a a)
a (+ a a a)]
a)
as being like so: (let [a 10]
(let [a (+ a a)]
(let [a (+ a a a)]
a)))
Which you can now re-imagine as the functional composition: (+ (+ 10 10) (+ 10 10) (+ 10 10))
So it has evaluation semantics which allow you to reduce it as described in lambda calculus using α-conversion and β-reduction.And this is different to the imperative style, because in imperative you can do:
int a = 10;
a = ++a + ++a;
Where `a` is now equal to 23, but in Clojure: (let [a 10
a (+ (inc a) (inc a))]
a)
You now have `a` equal to 22 instead.This is the difference between the variable substitution evaluation semantic of lambda calculus and the imperative semantics.
The println in the example is imperative, as it has side-effects, but a let block having an ordering of bindings is not inherently imperative. You can replace any let block with a set of nested functions:
(let [foo 12
bar (+ foo 100)]
[foo bar])
((fn [foo]
((fn [bar]
[foo bar])
(+ foo 100)))
12)
Sequential ordering of 'statements' is not automatically imperative. On the other hand, atoms are imperative, as they are mutable and therefore side-effectful.Language is hard.