On Thu, Jul 2, 2009 at 2:13 PM, Laurent PETIT<[email protected]> wrote:
>
> There are also peek and pop functions in core that you could you to
> convey the semantics of using the cons list as a stack.
Yes, that's a nice improvement! Here's the new code without the tests
which didn't change. Now it is using a vector instead of a list. Too
bad there is no push function in core. I'm using conj.
(defn- match [prev-char next-char]
(condp = prev-char
\( (= next-char \))
\[ (= next-char \])
false))
(defn- balanced-helper [s stack]
(if (empty? s)
(empty? stack)
(let [c (first s)
top (peek stack)
stack (if (match top c) (pop stack) (conj stack c))]
(recur (next s) stack))))
(defn balanced? [s] (balanced-helper s []))
(doseq [arg *command-line-args*]
(println (balanced? arg)))
> As far as performance is on the table, I'm not sure whether of
> cons'ing over lists or conj'ing over vectors would have the better
> performance (maybe it's just comparable ! :-)
--
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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---