Probably the following is much less efficient than the other solutions
proposed, but I find it easier to understand (and if I didn't
misunderstand the problem it gives the right answer).

(defn simplify-1
  "remove adjacent pairs of opening/closing brackets"
  ([string] (simplify-1 "" string))
  ([prefix [a & [b & cdef :as bcdef]]]
     (cond (nil? a) prefix
           (#{"()" "[]"} (str a b)) (recur prefix cdef)
           :otherwise (recur (str prefix a) bcdef))))

(defn simplify [s]
  (let [ss (simplify-1 s)]
    (if (= ss s) ss (recur ss))))

(defn balanced? [s]
  (empty? (simplify s)))

Cheers,

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