1. Ok, I'll consider that. 2. Yes, I'll post a link when I have uploaded the code somewhere.
3. It has not yet arrived 4. No. I have two sources of inspiration. Pattern matching in PLT Scheme and this link: http://groups.csail.mit.edu/mac/users/gjs/6.945/psets/ps05/ps.txt (which is almost SICP as it is written by Sussman) On Wed, Jul 8, 2009 at 7:15 PM, Sean Devlin<francoisdev...@gmail.com> wrote: > > 4. Is this example from SICP? > > On Jul 8, 12:12 pm, Sean Devlin <francoisdev...@gmail.com> wrote: >> This seems like a good use for a macro. A couple of thoughts: >> >> 1. Use arrays instead of lists >> In clojure, it is "best practice" to use arrays for data. So, your >> macro call should look like this. >> >> (match [1 2 3] >> [1 x] (+ x x) >> [1 x y] (+ x y)) >> >> 2. Could you post the source to match maker? That would help my play >> around in a REPL >> >> 3. As for books go, get yourself a copy of "Programming Clojure" by >> Stuart Holloway >> >> Sean >> >> On Jul 8, 11:42 am, Jonas <jonas.enl...@gmail.com> wrote: >> >> > Hi. >> >> > I'm developing a simple pattern matching library for clojure but I am >> > having >> > trouble with macros (which I have almost zero experience with). >> >> > I have a function `make-matcher` >> >> > (make-matcher <pattern>) >> >> > which returns a function that can pattern match on data and returns a >> > map of bindings (or nil in case of a non-match). >> >> > ((make-matcher '(list x y z w)) (list 1 2 3 4)) >> > ; => {x 1 y 2 z 3 w 4} >> > ((make-matcher '(list x y z x)) (list 1 2 3 4)) >> > ; => nil >> > ((make-matcher '(list 1 x 2 _)) (list 1 2 3 4)) >> > ; => nil >> > ((make-matcher '(list 1 x 2 _)) (list 1 3 2 9)) >> > ; => {x 3} >> >> > I have been trying to write the following 'match' macro: >> >> > (match <data> >> > <pattern-1> <body-1> >> > <pattern-2> <body-2>) >> >> > The macro should work like this: >> >> > (match (list 1 2 3) >> > (list 1 x) (+ x x) >> > (list 1 x y) (+ x y)) >> > ; => 5 >> >> > I have the following macros (none of which works correctly): >> >> > ; (letmap {a 1 b 2} (+ a b)) >> > ; =(should) expand to=> >> > ; (let [a 1 b 2] (+ a b)) >> > (defmacro letmap [dict & body] >> > `(let ~(into [] (reduce concat (eval dict))) >> > (do ~...@body))) >> >> > ; (match (list 1 2 3) >> > ; (list 1 x) (+ x x) >> > ; (list 1 x y) (+ x y)) >> > ; =should expand to something like=> >> > ; (let [dict (<matcher..> (list 1 2 3))] >> > ; (if dict >> > ; (letmap dict (+ 1 x)) >> > ; (match (list 1 2 3) (list 1 x y) (+ x y)))) >> > (defmacro match [data & clauses] >> > (when clauses >> > (let [pattern (first clauses) >> > body (second clauses) >> > matcher (make-matcher pattern)] >> > `(let [dict# (~matcher ~data)] >> > (if dict# >> > (letmap dict# ~body) >> > (match ~data (next (next ~clauses)))))))) >> >> > Any help is appreciated. Also pointers to documents and books where I >> > can >> > learn more about macros. >> >> > Thanks. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---