Ooo -- postwalk-replace, which I didn't know about, is much better than what I 
was going to suggest! But since this still might be of some interest to the OP 
here is the message I had composed before ajuc's message came through:

---
Setq doesn't do that in Lisp, but subst does.

I've defined subst in Clojure as follows, using the zipper library which I make 
available by including (:require [clojure.zip :as zip]) in my namespace 
declaration. I guess you could use "use" instead and avoid the "zip/" prefixes. 
BTW if you're doing genetic programming in Clojure then you might want to take 
a look at my Clojure genetic programming system 
(http://hampshire.edu/lspector/clojush/) although that evolves Push programs 
rather than standard s-expressions.

(defn subst
  "Returns the given list but with all instances of this (at any depth)         
                                                                                
                                                                                
         
replaced with that."
  [this that lst]
  ; must traverse twice, using a unique token as an intermediary, to avoid 
non-termination                                                                 
                                                                                
             
  ; with cases like (subst '(a b c) 'b '(a b c))                                
                                                                                
                                                                                
         
  (let [unique-token (gensym)
        with-token (loop [loc (zip/seq-zip lst)]
                     (if (zip/end? loc)
                       (zip/root loc)
                       (recur (zip/next (if (= (zip/node loc) that)
                         (zip/replace loc unique-token)
                         loc)))))]
    (loop [loc (zip/seq-zip with-token)]
      (if (zip/end? loc)
        (zip/root loc)
        (recur (zip/next (if (= (zip/node loc) unique-token)
                           (zip/replace loc this)
                           loc)))))))
---

 -Lee


On May 14, 2010, at 8:17 AM, ajuc wrote:

> Setq isn't functional - equivalent in clojure would be def, but it
> isn't meant to be used in that way.
> 
> For your purpose there is better fit: postwalk-replace
> 
> http://richhickey.github.com/clojure/clojure.walk-api.html#clojure.walk/postwalk-replace
> 
> Greetings.
> 

--
Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspec...@hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438

Check out Genetic Programming and Evolvable Machines:
http://www.springer.com/10710 - http://gpemjournal.blogspot.com/

-- 
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

Reply via email to