On Wed, Dec 15, 2010 at 11:22 AM, Poojan <poojanshah2...@gmail.com> wrote:
> I was trying to print out a list of symbols and I was wondering if I
> could remove the quotes.
>
> (def process-print-list
>  (fn [a-list]
>  (cond (empty? a-list) 'false
>  (list? a-list) (let [a a-list] (println (first a)) (process-print-
> list (rest a) ))
>  :else (process-print-list (rest a-list) ))))
>
> the list is ('x 'y 'z))
>
> with the following output:
>
> (quote x)
> (quote y)
> (quote z)

I'm not sure why you have the let in there. It's also usually better
to work on generic sequences than specifically on lists -- I almost
never use list? in production code. Indeed yours still traverses
non-list seqs but doesn't print anything for them.

As for the output, I don't see that happening with the code you
posted. I *do* see that happening if ('x 'y 'z) is being passed to a
macro that then calls process-print-list itself, at macroexpansion
time, rather than emitting code that calls process-print-list. If you
are passing symbols to a macro you usually DON'T want to quote them --
i.e. (my-macro (x y z)) rather than (my-macro ('x 'y 'z)) is probably
the sort of change you want to make. Macros don't evaluate their
arguments, they just plop them into their output verbatim (or can even
traverse their s-expression structure for various purposes) and the
output gets evaluated later.

(defn foo []
  (my-macro ('x 'y 'z)))

(foo)

When the defn form is evaluated, my-macro is called and expands into
something. At this time the list ('x 'y 'z) is not evaluated; it
likely just ends up in the macro output (the final code for the
function foo) somewhere.

When foo is called later on, it gets evaluated.

If the macro is printing its argument via process-print-list (say as a
debugging tool), the argument really is ((quote x) (quote y) (quote
z)); 'x is just shorthand for (quote x). (You might want to look into
using macroexpand-1 as a debugging tool, too.)

When the function foo evaluates the expansion, it will end up with the
actual symbol objects x, y, and z, also. If you have something like

(defn foo [x y z]
  (my-macro ('x 'y 'z)))

(foo 1 2 3)

the arguments to foo will end up unused (unless the macro plays around
with &env and its quoted argument). Again you probably then want

(defn foo [x y z]
  (my-macro (x y z)))

(foo 1 2 3)

whereupon in my-macro the argument will actually be a list of three
symbols, x, y, and z, which will get plopped into the output
somewhere, and when foo is called the evaluation of those symbols will
yield the function's three arguments.

-- 
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

Reply via email to