Clojure is immutable — what this really means

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

Damn, this is great:

Here’s the difference:

# python
def foo():
x = 23
y = lambda: x
x = 44
return y

print foo()()

# ruby
def foo():
x = 23
y = lambda { x }
x = 44
y

puts foo.call

// javascript
var foo = function(){
var x = 23;
var y = function(){ return x; }
x = 44;
return y;
};
alert( foo()() );

; clojure
(defn foo []
(let [x 23
y (fn [] x)
x 44]
y))

(println ((foo)))

One of these things is not like the others… one of these things is not the same. (I can’t remember which childhood TV show that’s from).

Three of the snippets above print 44. The other prints 23. That may not seem very significant, but it is. Immutability runs deeper than just unmodifiable vectors and maps.

Post external references

  1. 1
    http://chrisperkins.blogspot.com/2011/04/clojure-does-not-have-variables.html
Source