A single issue here: This (defn bal-parens? [parens] (let [replaced (clojure.string/replace parens "()" "") checked (re-seq #"\(\)" replaced)] (println checked) (if-not (nil? checked) (bal-parens? replaced) (do (println (str "replaced is " replaced)) (empty? replaced)))))
is closer to what you'd expect (note I added "do" in the if-not clause). An other flaw is using bal-parens? inside of bal-parens? which will not get translated into tail-recursion. Below you'll find a solution using loop-recur construct - the Clojure way of providing tail-recursion. (defn bal-parens? [parens] (loop [str parens] (if (= -1 (.indexOf str "()")) (empty? str) (let [replaced (clojure.string/replace str "()" "")] (recur replaced))))) Best, Maciej On 5 October 2014 00:02, <empty...@gmail.com> wrote: > Hi, > > I am trying to write a function to return a boolean as to whether > parenthesis are balanced or not? > > (defn bal-parens? [parens] > (let [replaced (clojure.string/replace parens "()" "") > checked (re-seq #"\(\)" replaced)] > (println checked) > (if-not (nil? checked) (bal-parens? replaced)) > (println (str "replaced is " replaced)) > (if (empty? replaced) true false))) > > (bal-parens? "(()())") > > I'd expect to see true here. > > But my output is: > > (()) > nil > replaced is > replaced is () > false > > I can't for the life of me think why, that when checked is nil and replaced > is empty the function doesn't terminate! > > Many Thanks > > Aidy > > -- > 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 > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.