On Sat, Jan 8, 2011 at 7:25 PM, Andreas Kostler
<andreas.koestler.le...@gmail.com> wrote:
> Ken,
> Thanks for your reply. Is that about what you mean?
>
> (defmacro literal? [form]
>  (list 'if `(not (list? ~form)) true false))

Sort of, but that isn't quite going to work. It will expand to

(if (clojure.core/not (clojure.core/list? <the-form>)) true false)

That's not especially efficient; you can dispense with the if entirely
and just use

`(not (list? ~form))

and get the same results. But the results also won't be what you're
hoping for. Put in

(literal? (+ 2 3))

and you'll get true! That's because it will expand to

(not (list? (+ 2 3)))

which will evaluate to (not (list? 5)) and then (not false) and then
true. 5 is not a list.

The problem is that the list? test is happening at run time, not
macroexpansion time. You'd need

(defmacro literal? [form]
  (not (list? form)))

which will simply evaluate to true or false depending.

(literal? (+ 2 3))

will now expand to false, and that will evaluate to false.

I'm not sure what use this is, though. I suppose you want to know if
an argument to some more complex macro is literal or not, in which
case (not (list? arg)) (OUTSIDE of your syntax-quotes, or unquoted)
will do nicely, depending on your requirements.

But do you want [1 2] and [(- 3 2) (+ 1 1)] to both count as literal,
or only the former? What about [(count my-seq) (reduce + (map
parse-int (line-seq rdr)))]? Do you want (quote (1 2 3)) to count as
literal?

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