On Thu, Jul 2, 2009 at 1:59 PM, Stuart
Halloway<stuart.hallo...@gmail.com> wrote:
>
> Hi Mark,
>
> The balanced-test would be a great place to use "are" instead of "is".

Excellent suggestion!  The new version of that function follows:

(deftest balanced-test
  (are [text result]
    (= (balanced? text) result)
    "()" true
    "[]" true
    "([])" true
    "[()]" true
    "[]()" true
    "[][[([])]]" true
    "(" false
    ")" false
    "[" false
    "]" false
    "][" false
    ")(" false
    "( )" false
    "([)" false
    "[)]" false
    "([)]" false
    "({})" false
    "[())]") false)

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



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

Reply via email to