This is only an example, I understand this can easily be achieved with a function.
Don't Work
This is what I've come up with based on the elisp manual and a couple of answers/articles that I have read. The suggestion is to use gensym
, but I can't get it to output '("bread")
, instead I get '(g719)
.
(defmacro shop (item)
(let ((var (gensym)))
`(let ((,var ,item ))
(add-to-list 'shopping-list '(,var)))))
(let ((item "bread"))
(macroexpand '(shop item)))
RESULTS:
(let
((g719 item))
(add-to-list 'shopping-list
'(g719)))
Works but...
This works when I rename the variable from item
to my-item
, but I would like to use
the same name. I've read you're not suppose to use eval. Not sure about symbol-value?
(defmacro shop (item)
`(add-to-list 'shopping-list '(,(symbol-value item))))
(let ((my-item "bread"))
(macroexpand '(shop my-item)))
RESULTS:
(add-to-list 'shopping-list
'("bread"))