On Thu, Jul 2, 2009 at 2:46 PM, Stephen C. Gilardi<squee...@mac.com> wrote: > > On Jul 2, 2009, at 3:21 PM, Mark Volkmann wrote: > >> Now it is using a vector instead of a list. Too >> bad there is no push function in core. I'm using conj. > > conj is the correct thing to use for the push operation.
Right, but the point Laurent was making is that using peek and pop are good because they convey the fact that I'm using a vector as a stack. For that reason it would be nice if there was a push function to go along with peek and pop. > Here's my take on it based on yours. This bails early as soon as there is a > mismatch or an illegal character: Yeah, I realized mine wasn't doing that, but was happy that it gave the correct result anyway since the illegal characters get added to the stack and are never popped off. Your implementation is certainly more efficient though. > (ns challenge > (:use clojure.test)) > > (def lefts #{\( \[}) > (def matches {\( \) \[ \]}) Cool use of a set and a map! > (defn balanced? [s] > (loop [s s stack ()] > (if (seq s) Interesting use of seq. (empty? s) seems more clear to me though. > (let [[c & s] s] > (if (lefts c) > (recur s (conj stack c)) > (if (= (matches (peek stack)) c) > (recur s (pop stack)) > false))) > (empty? stack)))) > > (defn main [] > (run-tests 'challenge)) Very nice! Thanks for sharing! -- 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 -~----------~----~----~----~------~----~------~--~---