Hi, I think you could use recur instead of the direct recursive call,
Regards, -- Laurent 2009/7/2 Mark Volkmann <r.mark.volkm...@gmail.com>: > > There is a challenge on the blog of Tony Morris at > http://dibblego.wordpress.com/2008/09/05/haskell-scala-java-7-functional-java-java/#comment-2460. > It's a parsing problem for which he compares solutions in Haskell, > Scala and Java. I added a Clojure solution. I don't know if this is > the "best" way to solve this with Clojure, but it certainly works. > Here's my code, including unit tests. > > (use 'clojure.test) > > (defn- match [prev-char next-char] > (condp = prev-char > \( (= next-char \)) > \[ (= next-char \]) > false)) > > ; Need a better name for this function. > (defn- helper [s stack] > (if (empty? s) > (empty? stack) > (let [c (first s) > top (first stack) > stack (if (match top c) (next stack) (cons c stack))] > (helper (next s) stack)))) > > (defn balanced? [s] (helper s '())) > > (doseq [arg *command-line-args*] > (println (balanced? arg))) > > (deftest match-test > (is (match \( \))) > (is (match \[ \])) > (is (not (match \( \{)))) > > (deftest balanced-test > (is (balanced? "()")) > (is (balanced? "[]")) > (is (balanced? "([])")) > (is (balanced? "[()]")) > (is (balanced? "[]()")) > (is (balanced? "[][[([])]]")) > (is (not (balanced? "("))) > (is (not (balanced? ")"))) > (is (not (balanced? "["))) > (is (not (balanced? "]"))) > (is (not (balanced? "]["))) > (is (not (balanced? ")("))) > (is (not (balanced? "( )"))) > (is (not (balanced? "([)"))) > (is (not (balanced? "[)]"))) > (is (not (balanced? "([)]"))) > (is (not (balanced? "({})"))) > (is (not (balanced? "[())]")))) > > (run-tests) > > -- > R. Mark Volkmann > Object Computing, Inc. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---