Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

Hello,

I am a Clojure beginner, I don't understand the concept of immutable data.
Because the complier allow me to def the 'girl1' more then 1 time below:

(when true
  (def girl1 "stephanie")
  (def girl1 "ann")
  (def girl3 "victoria")
  (println girl1 girl3))

Output

ann victoria
nil

1 Answer

0 votes
by

def creates a var, which is stateful (mutable) box referring to a value. The values here are all strings, which are immutable.

by
thanks @alexmiller
By the way, what if the values are not strings, eg. long.

(when true
  (def girl4 4)
  (def girl4 5)
  (def girl5 6)
  (println girl4 girl5 )
  )

The values are immutable too?
by
yes, numbers are immutable, but that's true of most (all?) languages. The interesting thing in Clojure is that collections are immutable.

(def a-vec [1 2 3])

(conj a-vec 4)
;;=> [1 2 3 4]

a-vec
;;=> [1 2 3]
...