Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Asked
Modified today
Viewed 51 times
2

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:

  1. Write hello-world using a defun rather than a defmacro.

  2. Use append.

  3. Delete nil from FORM before the defmacro returns it.

  4. Stay in good-mood permanently :-)

1 Answer 1

4

Just use a splice instead, but note that in the true case you need to supply a list of forms rather than a single form. See chapter 10.4 Backquote of the Emacs Lisp Reference Manual.

,@(if CONDITION (list SUB-FORM))

Thus, adapting your example:

(defmacro hello-world-min (good-mood)
 `(progn
   (princ "Hello")
   ,@(if good-mood '((princ " wonderful")))  ;; Here is the test involving the SUB-FORM (princ " cruel")
   (princ " world")))
1
  • Wonderful! Thanks!
    Ruy
    –  Ruy
    2025-12-05 16:04:28 +00:00
    Commented 12 hours ago

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.