Chick Corea wrote: > Is everything in Clojure immutable? Most things. Clojure tries very hard to be thread-safe by default.
> how does one modify the value of 'x' ? > > (let [x nil] (def x true)) One does not, at least not in a let-binding. If you really want a lexical variable you can get one with a var: (with-local-vars [x 3] (println (var-get x)) (var-set x 5) (println (var-get x))) It is thread-local to enforce thread-safety. But as the others have said you should really be doing things functionally not imperatively. I've actually never used with-local-vars in a real program. > So I want to ask, am I in the minority in thinking that read-only > local bindings limit > the programmer a lot. You probably just have "gotten" functional programming yet. If you use functions like map, reduce and for and recursion instead of traditional looping you basically find yourself not needing mutable local bindings. > Do both of these statements intern a symbol? Or does the 2nd set the > currently > interned symbol to a new value ? > > (def x 1) > (def x 2) This changes the root binding of the var named by x to a new value, it does not create a new var. The reason Clojure allows this is to make interactive development and debugging easier (so you can redefine a function at run-time). user> (def x 1) user> (def a #'x) user> (def x 2) user> (identical? a #'x) true user> a #'user/x user> (type a) clojure.lang.Var > Is the proper way to change the value bound to a symbol to use > "set!" ? > > (set! x 3) > If I understand correctly, the "(binding ...)" macro specifically > applies to > "Variables", one of Clojure's STM types, thus has dynamic, thead-local > scope. Is that right? In which case, it may hide a lexical binding ? Yes, I think you are basically correct. Maybe this example will help: (def foo 5) ; thread-global nearly immutable root binding (binding [foo 7] ; creates a thread-local binding for x (set! foo 9) ; changes the thread-local binding (let [t (Thread. (fn [] (println "foo in other thread:" x) (set! foo 25)))] (.start t) (.join t)) (println "foo in main thread:" foo) (def foo 72) ; changing the root-bind (don't do this!) (println "foo in main thread after 2nd def:" foo)) (println "foo outside binding:" foo) Output: x in other thread: 5 Exception in thread "Thread-133" java.lang.RuntimeException: java.lang.IllegalStateException: Can't change/establish root binding of: foo with set x in main thread: 9 x in main thread after 2nd def: 9 foo outside binding: 72 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---