I were trying to make a macro for binding variable if it is unbind, else if it is binded than appending to its value
(defmacro set-or-nconc (var &rest args)
`(,(if (and (boundp var) (not (null ,var)))
'nconc 'setq)
,var ,@args))
The intended output wanted is
(set-or-nconc i '(a b)) => '(a b)
i => '(a b)
(set-开发者_如何转开发or-nconc i '(a b)) => '(a b a b)
i => '(a b a b)
But my defined macro is not working especially if i is binded to nil Do somebody have idea what is the problem with this macro ?
The determination as to use setq
or nconc
is done at macro-expansion time, not at run-time. This is a bit of a problem. There's also some issues with your backquote expression, as there's either a "," too many (in (null ,var)
) or one too few (in (boundp var)
, with the need for another backquote).
Below is at least something approaching a working solution, delaying the choice of using setq
or nconc
until run-time:
(defmacro set-or-nconc (var &rest args)
`(if (and (boundp ',var) (not (null ,var)))
(nconc ,var ,@args)
(setq ,var ,@args)))
Also be aware that using this to gradually build up a list will have more-or-less O(n^2) behaviour, it's usually faster to accumulate by consing, then (if necessary) do a reverse at the end.
精彩评论