Re: parsing program for nested parens and square brackets

2009-07-04 Thread Russell Christopher
(def matches {\( \) \[ \]}) (defn balanced? [s] (empty? (reduce #(if (= (matches (peek %1)) %2) (pop %1) (conj %1 %2)) [] s))) Learning Clojure. So far I'm really liking it. This is the first time I've tried anything outside of some REPL incantations from books, blogs, this list, etc thus it wo

Re: parsing program for nested parens and square brackets

2009-07-03 Thread carlitos
Probably the following is much less efficient than the other solutions proposed, but I find it easier to understand (and if I didn't misunderstand the problem it gives the right answer). (defn simplify-1 "remove adjacent pairs of opening/closing brackets" ([string] (simplify-1 "" string))

Re: parsing program for nested parens and square brackets

2009-07-03 Thread Laurent PETIT
2009/7/3 Christophe Grand : > Hi all! > > On Thu, Jul 2, 2009 at 11:09 PM, Laurent PETIT > wrote: >> >> (def push conj) >> (def closing-counterpart { \( \), \[ \] }) >> (def opening-char? closing-counterpart) >> (defn matching-pair? [left right] (= right (closing-counterpart left))) >> >> (defn c

Re: parsing program for nested parens and square brackets

2009-07-03 Thread Christophe Grand
Hi all! On Thu, Jul 2, 2009 at 11:09 PM, Laurent PETIT wrote: > (def push conj) > (def closing-counterpart { \( \), \[ \] }) > (def opening-char? closing-counterpart) > (defn matching-pair? [left right] (= right (closing-counterpart left))) > > (defn consume-one [stack c] > (if (or (opening-char

Re: parsing program for nested parens and square brackets

2009-07-03 Thread B Smith-Mannschott
On Fri, Jul 3, 2009 at 00:50, Laurent PETIT wrote: > > Hey, how come we did not see this even more concise version sooner ? :-): > > ;; using just clojure 1.0.0 without any additional library :-) > ;; from command line: > ;; java -cp clojure.jar /path/to/challenge2.clj "()" "((([[]])))" ... ... >

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Laurent PETIT
Hey, how come we did not see this even more concise version sooner ? :-): ;; using just clojure 1.0.0 without any additional library :-) ;; from command line: ;; java -cp clojure.jar /path/to/challenge2.clj "()" "((([[]])))" ... ... (ns challenge2) (defn balanced? [s] (and (every? #{ \( \)

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Mark Tarver
This is a solution in Qi+Qi-YACC (define test-brackets X -> (not (= (compile (COERCE X LIST)) fail!))) (defcc #\[ #\] ; #\( #\) ; ; := [];) (defcc -*- := (if (element? -*- [#\[ #\] #\( #\)]) #\Escape -*-);) Testing (5-) (test-brackets "()") true (6-) (test-brackets "())")

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Mark Tarver
Here is a solution in Qi+Qi YACC. (define test-brackets X -> (not (= (compile (COERCE X LIST)) fail!))) (defcc #\[ #\] ; #\( #\) ; ; := [];) (defcc -*- := (if (element? -*- [#\[ #\] #\( #\)]) #\Escape -*-);) Mark --~--~-~--~~~---~--~~ You rec

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Laurent PETIT
Hi, OK, so here's my attempt at doing this the most higher order but also the most understandable I could do. Hope you enjoy reading it as much as I enjoyed polishing it :-) : ;; from command line: ;; java -cp clojure.jar /path/to/challenge.clj "()" "((([[]])))" ... ... (ns challenge) (def pus

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Mark Volkmann
On Thu, Jul 2, 2009 at 2:46 PM, Stephen C. Gilardi wrote: > > On Jul 2, 2009, at 3:21 PM, Mark Volkmann wrote: > >> Now it is using a vector instead of a list. Too >> bad there is no push function in core. I'm using conj. > > conj is the correct thing to use for the push operation. Right, but the

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Stephen C. Gilardi
On Jul 2, 2009, at 3:21 PM, Mark Volkmann wrote: Now it is using a vector instead of a list. Too bad there is no push function in core. I'm using conj. conj is the correct thing to use for the push operation. Here's my take on it based on yours. This bails early as soon as there is a misma

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Mark Volkmann
On Thu, Jul 2, 2009 at 2:13 PM, Laurent PETIT 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 vect

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Laurent PETIT
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. 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 ! :

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Mark Volkmann
On Thu, Jul 2, 2009 at 2:04 PM, Laurent PETIT wrote: > > Hi, > > I think you could use recur instead of the direct recursive call, Great idea! Simply changing "helper" to "recur" works. > 2009/7/2 Mark Volkmann : >> >> There is a challenge on the blog of Tony Morris at >> http://dibblego.wordpre

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Mark Volkmann
On Thu, Jul 2, 2009 at 1:59 PM, Stuart Halloway 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) "()" tru

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Laurent PETIT
Hi, I think you could use recur instead of the direct recursive call, Regards, -- Laurent 2009/7/2 Mark Volkmann : > > 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 proble

Re: parsing program for nested parens and square brackets

2009-07-02 Thread Stuart Halloway
Hi Mark, The balanced-test would be a great place to use "are" instead of "is". Cheers, Stu > > 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 com

parsing program for nested parens and square brackets

2009-07-02 Thread Mark Volkmann
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