Re: Newbie macro problems
2. http://gist.github.com/142939 On Wed, Jul 8, 2009 at 7:19 PM, Jonas Enlund wrote: > 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 wrote: >> >> 4. Is this example from SICP? >> >> On Jul 8, 12:12 pm, Sean Devlin 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 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 ) >>> >>> > 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 >>> > >>> > ) >>> >>> > 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 ( (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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
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 wrote: > > 4. Is this example from SICP? > > On Jul 8, 12:12 pm, Sean Devlin 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 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 ) >> >> > 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 >> > >> > ) >> >> > 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 ( (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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
2009/7/8 Sean Devlin : > > Laurent, > > I don't quite understand your point. Could you please explain it a > little more? Oh, I wanted to be quick. I think using the term "array" instead of "vector" (which is the real term used for datastructure created by a [:a :b :c :d] form) may be confusing (one could overlook and understand "java array"). see: http://clojure.org/data_structures#toc15 and from ( http://clojure.org/reader ): "Vectors Vectors are zero or more forms enclosed in square brackets: [1 2 3] " Regards, -- Laurent > > Thanks > > On Jul 8, 12:16 pm, Laurent PETIT wrote: >> 2009/7/8 Sean Devlin : >> >> >> >> > 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)) >> >> Hi, >> >> s/array/vector/g >> >> Regards, >> >> -- >> Laurent > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
Good point. I'll be careful to use the term vector in the future, and array for java interop only. On Jul 8, 12:30 pm, Laurent PETIT wrote: > 2009/7/8 Sean Devlin : > > > > > Laurent, > > > I don't quite understand your point. Could you please explain it a > > little more? > > Oh, I wanted to be quick. > > I think using the term "array" instead of "vector" (which is the real > term used for datastructure created by a [:a :b :c :d] form) may be > confusing (one could overlook and understand "java array"). > > see:http://clojure.org/data_structures#toc15 > > and from (http://clojure.org/reader): > "Vectors > Vectors are zero or more forms enclosed in square brackets: > [1 2 3] > " > > Regards, > > -- > Laurent > > > > > Thanks > > > On Jul 8, 12:16 pm, Laurent PETIT wrote: > >> 2009/7/8 Sean Devlin : > > >> > 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)) > > >> Hi, > > >> s/array/vector/g > > >> Regards, > > >> -- > >> Laurent --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
Laurent, I don't quite understand your point. Could you please explain it a little more? Thanks On Jul 8, 12:16 pm, Laurent PETIT wrote: > 2009/7/8 Sean Devlin : > > > > > 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)) > > Hi, > > s/array/vector/g > > Regards, > > -- > Laurent --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
2009/7/8 Sean Devlin : > > 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)) Hi, s/array/vector/g Regards, -- Laurent --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
4. Is this example from SICP? On Jul 8, 12:12 pm, Sean Devlin 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 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 ) > > > 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 > > > > ) > > > 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 ( (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 -~--~~~~--~~--~--~---
Re: Newbie macro problems
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 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 ) > > 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 > > ) > > 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 ( (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 -~--~~~~--~~--~--~---
Newbie macro problems
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 ) 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 ) 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 ( (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 -~--~~~~--~~--~--~---