Created
November 7, 2012 21:42
-
-
Save rrees/4034692 to your computer and use it in GitHub Desktop.
Balanced parens example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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? "(())") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment