When writing a macro one often constructs the form to be evaluated, henceforth referred to as FORM, using the back-quote list construct.
So suppose that, in the middle of this process, one is about to add an element to FORM (typically another form), henceforth referred to as SUB-FORM, which, depending on a certain CONDITION (presumably a test based on some macro parameter), may perhaps make the addition of this SUB-FORM unnecessary. My gut instinct is to write
(defmacro NAME (VARS)
`(... ,(if CONDITION SUB-FORM) ...)
As a result, when CONDITION fails, a "nil" is added to FORM. This is actually no big deal since, when FORM goes through eval-time, this undesired nil is evaluated, but this is not likely to be relevant in the finall result.
Still (perhaps being a bit of a purist) the presence of this unnecessary nil annoys me and my question has to do with possible ways to eliminate it.
I can of course think of lots of ways to do it, but I'd like to know if anyone has though of a specially elegant solution such as a variation of the function if, say if2, such that the construct
,(if2 CONDITION SUB-FORM)
would leave no trace in FORM upon the failure of CONDITION.
Here is a toy example illustrating the presence of this unwanted nil.
(defmacro hello-world (good-mood)
`(progn
(princ "Hello")
,(if good-mood '(princ " wonderful")) ;; Here is the test involving the SUB-FORM (princ " cruel")
(princ " world")))
When in bad mood, FORM, namely
(macroexpand '(hello-world nil))
becomes
(progn (princ "Hello") nil (princ " world"))
and you can see the innocuous but undesired nil showing up.
PS: In order to save your time let me say that I have already thought of solutions along the lines of:
Write
hello-worldusing adefunrather than adefmacro.Use
append.Delete
nilfromFORMbefore thedefmacroreturns it.Stay in good-mood permanently :-)