Re: with-gensyms
On Nov 20, 4:18 pm, Rock <[EMAIL PROTECTED]> wrote: > Another question: I was wondering why there seem to be no macrolet or > symbol-macrolet macros in the language. Is it part of the language > design or will they be added in future releases? It's been discussed, but not implemented: http://groups.google.com/group/clojure/browse_thread/thread/db324a87e967537a/fed7356d126bc720 Since Clojure macros are just functions with {:macro true} metadata, you might be able to do macrolets with let: UNTESTED CODE: (defn my-function [x y z] (let [ff (with-meta (fn [x] ...) {:macro true})] ...)) -Stuart Sierra --~--~-~--~~~---~--~~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: with-gensyms
On Nov 20, 2008, at 4:18 PM, Rock wrote: > Another question: I was wondering why there seem to be no macrolet or > symbol-macrolet macros in the language. Is it part of the language > design or will they be added in future releases? Rich misses symbol-macrolet too: http://clojure-log.n01se.net/date/2008-06-16.html It's on the to-do list in the long-term section: http://richhickey.backpackit.com/pub/1597914 --Steve --~--~-~--~~~---~--~~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: with-gensyms
Well, I'm no Clojure macro expert yet, that's for sure, but from what I gather the auto-gensym mechanism has limitations to some extent, so that using regular gensyms in certain cases appears to be mandatory. So, I figured, something like good old WITH-GENSYMS could come in handy. I'm not certain my version is written correctly, and, above all, in a decent Clojure style. Still learning. Hope that answers your question. Rock On Nov 20, 9:50 pm, Allen Rohner <[EMAIL PROTECTED]> wrote: > On Nov 20, 1:58 pm, Rock <[EMAIL PROTECTED]> wrote: > > > > > I've done my best to get a good enough grasp of how macros and syntax- > > quotes work in Clojure. While waiting for some more detailed > > documentation (thanks Meikel), I thought I'd work on something > > practical. Here's my attempt at writing a version of WITH-GENSYMS for > > Clojure, taken directly from Peter Seibel's Practical Common Lisp: > > > (defmacro with-gensyms [[& names] form] > > `(let ~(apply vector (loop [n names result []] > > (if (nil? n) > > result > > (recur (rest n) > > (conj result (first n) `(gensym)) > > ~form)) > > > Tried it out with macroexpand and it seems to work fine. Let me know > > what you all think. Perhaps something like with-gensyms could be > > included directly in the core lanuage. I've always thought that it > > should have been included in Common Lisp, given that it is employed so > > frequently. > > > Rock > > I'm new to macros as well. Can you please explain when this would be > useful? How is it different from the auto-gensym that the reader > provides ( foo#)? > > Allen --~--~-~--~~~---~--~~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: with-gensyms
Wow, I'm impressed. Thanks for the explanation Meikel. Clojure is indeed very expressive. Another question: I was wondering why there seem to be no macrolet or symbol-macrolet macros in the language. Is it part of the language design or will they be added in future releases? On Nov 20, 10:08 pm, Meikel Brandmeyer <[EMAIL PROTECTED]> wrote: > Hi Rock, > > Am 20.11.2008 um 20:58 schrieb Rock: > > > Peter Seibel's Practical Common Lisp: > > You might want to look at Stuart Halloway's "PCL->Clojure" > series:http://blog.thinkrelevance.com/2008/09/16/pcl-clojure > > > (defmacro with-gensyms [[& names] form] > > `(let ~(apply vector (loop [n names result []] > > (if (nil? n) > > result > > (recur (rest n) > > (conj result (first n) `(gensym)) > > ~form)) > > - [[& names]] is actually the same as [names]. The only difference > is that the former will give an error when you don't pass a > collection. > However that, you will get anyway later on... > > - (apply vector ...) can also be done via (vec ...). > > - The loop can also be done with reduce or mapcat. > > (defmacro with-gensyms > [names form] > `(let ~(vec (mapcat (fn [n] [n `(gensym)]) names)) > ~form)) > > or with reduce > > (defmacro with-gensyms > [names form] > `(let ~(vec (reduce #(conj %1 %2 `(gensym)) [] names)) > ~form)) > > or just like that... > > (defmacro with-gensyms > [names form] > `(let ~(vec (interleave names (repeat `(gensym > ~form)) > > So, many ways lead to Rome. :) I think the last one is maybe > the most elegant. > > I think for Clojure, this is not really interesting, because > you have the # notation to create automatic gen-syms on the > fly. This already gets you very far. gensym is only in special > cases necessary. > > (defmacro foo [x] (with-gensyms [xx] `(let [~xx ~x] ~xx))) > vs. > (defmacro foo [x] `(let [x# ~x] x#)) > > Sincerely > Meikel > > smime.p7s > 5KViewDownload --~--~-~--~~~---~--~~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: with-gensyms
Hi Rock, Am 20.11.2008 um 20:58 schrieb Rock: Peter Seibel's Practical Common Lisp: You might want to look at Stuart Halloway's "PCL->Clojure" series: http://blog.thinkrelevance.com/2008/09/16/pcl-clojure (defmacro with-gensyms [[& names] form] `(let ~(apply vector (loop [n names result []] (if (nil? n) result (recur (rest n) (conj result (first n) `(gensym)) ~form)) - [[& names]] is actually the same as [names]. The only difference is that the former will give an error when you don't pass a collection. However that, you will get anyway later on... - (apply vector ...) can also be done via (vec ...). - The loop can also be done with reduce or mapcat. (defmacro with-gensyms [names form] `(let ~(vec (mapcat (fn [n] [n `(gensym)]) names)) ~form)) or with reduce (defmacro with-gensyms [names form] `(let ~(vec (reduce #(conj %1 %2 `(gensym)) [] names)) ~form)) or just like that... (defmacro with-gensyms [names form] `(let ~(vec (interleave names (repeat `(gensym ~form)) So, many ways lead to Rome. :) I think the last one is maybe the most elegant. I think for Clojure, this is not really interesting, because you have the # notation to create automatic gen-syms on the fly. This already gets you very far. gensym is only in special cases necessary. (defmacro foo [x] (with-gensyms [xx] `(let [~xx ~x] ~xx))) vs. (defmacro foo [x] `(let [x# ~x] x#)) Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature
Re: with-gensyms
On Nov 20, 1:58 pm, Rock <[EMAIL PROTECTED]> wrote: > I've done my best to get a good enough grasp of how macros and syntax- > quotes work in Clojure. While waiting for some more detailed > documentation (thanks Meikel), I thought I'd work on something > practical. Here's my attempt at writing a version of WITH-GENSYMS for > Clojure, taken directly from Peter Seibel's Practical Common Lisp: > > (defmacro with-gensyms [[& names] form] > `(let ~(apply vector (loop [n names result []] > (if (nil? n) > result > (recur (rest n) > (conj result (first n) `(gensym)) > ~form)) > > Tried it out with macroexpand and it seems to work fine. Let me know > what you all think. Perhaps something like with-gensyms could be > included directly in the core lanuage. I've always thought that it > should have been included in Common Lisp, given that it is employed so > frequently. > > Rock I'm new to macros as well. Can you please explain when this would be useful? How is it different from the auto-gensym that the reader provides ( foo#)? Allen --~--~-~--~~~---~--~~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
with-gensyms
I've done my best to get a good enough grasp of how macros and syntax- quotes work in Clojure. While waiting for some more detailed documentation (thanks Meikel), I thought I'd work on something practical. Here's my attempt at writing a version of WITH-GENSYMS for Clojure, taken directly from Peter Seibel's Practical Common Lisp: (defmacro with-gensyms [[& names] form] `(let ~(apply vector (loop [n names result []] (if (nil? n) result (recur (rest n) (conj result (first n) `(gensym)) ~form)) Tried it out with macroexpand and it seems to work fine. Let me know what you all think. Perhaps something like with-gensyms could be included directly in the core lanuage. I've always thought that it should have been included in Common Lisp, given that it is employed so frequently. Rock --~--~-~--~~~---~--~~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---