On Fri, Aug 12, 2011 at 12:41 PM, daly <d...@axiom-developer.org> wrote: > Clojure has immutable data structures. > Programs are data structures. > Therefore, programs are immutable. > > So is it possible to create a Clojure program that modifies itself?
Yes, if it slaps forms together and then executes (eval `(def ~sym ~form)) or (eval `(defn ~sym ~argvec ~form)) or similarly, or perhaps uses alter-var-root. (May require :dynamic true set for the involved Vars in 1.3 for functions and such to start using the new values right away -- binding definitely does. In 1.2, alter-var-root should "just work". Changes occur as with atom's swap!, so the function passed to alter-var-root may potentially execute more than once.) Alternatively, you can create new functions on the fly by evaling fn forms and use atoms or refs to hold stored procedures in Clojure's other concurrency-safe mutability containers. These need to be called by derefing them, e.g. (@some-atom arg1 arg2). The functions will compile to bytecode and be eligible for JIT the same as ones not created dynamically at runtime, though the reference lookups carry a performance hit on invocation. Clojure can probably be quite a good AI research and development platform. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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