Toothrot
Toothrot

Reputation: 334

Common Lisp macro variable expansion

(defmacro foo (x)
    `(defun ,xt ()
         (format t "hullo")))

(foo bar)

will not define a function bart, since ,xt is read as the variable xt rather than the variable x plus a t. But is there a way to get a function bart by supplying the argument bar?

Upvotes: 0

Views: 131

Answers (1)

uselpa
uselpa

Reputation: 18917

You need to create the function name (which is a string then) and then convert it to a symbol, for example:

(defmacro foo (x)
  `(defun ,(intern (format nil "~aT" x)) ()
     (format t "hullo")))

then

? (foo bar)
BART
? (bart)
hullo
NIL

Upvotes: 3

Related Questions