The multiple syntax-quote and unqote behavior above seems to work in Clojure just fine and like CL as well.
On Mon, Jan 19, 2009 at 5:58 AM, Rock <[email protected]> wrote: > > I'm trying to make the documentation (still awaiting approval) in the > "Learning Clojure" WikiBook regarding syntax-quote expansion as > accurate as possible (like in the CL HyperSpec). > > I've recently noticed something. In "The Reader" section of the > Clojure Reference, where it explains syntax-quotes, it says: > > "For all forms other than Symbols, Lists, Vectors and Maps, `x is the > same as 'x." > > Yet, I noticed that for *values* such as numbers, strings etc... > that's not exactly so. Let's expand a syntax-quote expression like the > following: > > `(x 5) ===> (clojure.core/concat (clojure.core/list (quote user/x)) > (clojure.core/list 5)) > > See the difference between what happens to the symbol x ===> (quote > user/x) and what happens to 5? > > It looks like `5 gets expanded to 5 and not (quote 5). Now I know that > they evaluate to the same identical thing. But what I'm interested in, > is the precise expansion algorithm. > > So my first question is: > > 1) Is it true that, for values, we have for instance `5 ===> 5 in a > syntax-quote expansion (not evaluation!). > > There's another thing I'm curious about. Syntax-quotes are there to > simplify such things as wiriting macros (especially) for us. Now I'm > trying to be rigorous in the docs so that everything is clear. But the > whole reason for syntax-quotes is to make life easier, so there's no > point in someone having to mentally expand those expressions every > time even if he/she knows the precise rules. > > Paul Graham discusses this in Ansi Common Lisp. He also illustrates > there an equivalent way of expanding backquote expressions which is > truly useful and possibly less confusing for a human mind especially > when dealing with notoriously difficult nested backquotes (or syntax- > quotes in our own terminology :-) ). I think it was taken from Scheme, > but I'm not certain. Anyway it's pretty simple and straightforward. It > works like this: > > "To evaluate a backquoted expression, you remove the backquote > and each matching comma, and replace the expression following each > matching > comma with its value. Evaluating an expression that begins with a > comma causes an > error. > A comma matches a backquote if there are the same number of commas > as > backquotes between them, where b is between a and c if a is prepended > to an expression > containing b, and b is prepended to an expression containing c. This > means that in a > well-formed expression the outermost backquote matches the innermost > comma(s). > Suppose that x evaluates to a, which evaluates to 1; and that y > evaluates to b, > which evaluates to 2. To evaluate the expression > > ``(w ,x ,,y ) > > we remove the first backquote and evaluate what follows any matching > comma. The > rightmost comma is the only one that matches the first backquote. If > we remove it and > replace the expression it's prepended to, y, with its value, we get: > > `(w ,x ,b) > > and if we evaluate that in turn we get: > > (w a 2)" > > That's Common Lisp notation so mentally replace commas with ~. It took > me some getting used too! :-) > > Now my other question is this: > > 2) Given that in Clojure a syntax-quote (`) doesn't just quote a > *symbol* but *resolves* it in the current context, would the above > method still be applicable or is it compromised because of this? > That's something that isn't clear to me yet. > > Thanks. > > Rock > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] 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 -~----------~----~----~----~------~----~------~--~---
