Skip to content

Instantly share code, notes, and snippets.

@rrees
Created November 7, 2012 21:42
Show Gist options
  • Select an option

  • Save rrees/4034692 to your computer and use it in GitHub Desktop.

Select an option

Save rrees/4034692 to your computer and use it in GitHub Desktop.

Revisions

  1. rrees created this gist Nov 7, 2012.
    33 changes: 33 additions & 0 deletions gistfile1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@

    (defn balanced?
    ([s] (apply balanced? 0 (seq s)))

    ([current-count x & xs]
    (let [new-count
    (cond
    (= x \() (inc current-count)
    (= x \)) (dec current-count)
    :else current-count)]
    (if (< current-count 0) false
    (if xs
    (apply balanced? new-count xs)
    (= 0 new-count))))))


    (balanced? " Hello (world))")
    (balanced? "Hello )) world ((")

    (balanced? "(")
    (balanced? "(hello world")
    (balanced? "(hello world)")
    (apply balanced? 0 (seq "()())("))
    (balanced? "())(")

    (balanced? "()")
    (balanced? "(())")