Re: macro question



Karol Skocik asked:

I want in a macro to have in let section something like :

`(let ((added-nodes (make-hash-table)))
....

or, sometimes I need this :
`(let ((added-nodes (make-hash-table))
(undo-funcs '()))
...

I just can't use ,(when make-undo `(undo-funcs '()))
because it leaves nil in the list when the condition is
not true.

* (defmacro example(make-undo)
`(let ((added-nodes (make-hash-table))
,@(if make-undo
(list `(undo-funcs '()))
'()))
body-forms))

* (macroexpand-1 '(example nil))

(LET ((ADDED-NODES (MAKE-HASH-TABLE)))
BODY-FORMS)
T
* (macroexpand-1 '(example t))

(LET ((ADDED-NODES (MAKE-HASH-TABLE)) (UNDO-FUNCS 'NIL))
BODY-FORMS)
T

There is a standard Lisp idiom for implementing filters
using mapcan - wrap what you want in a list, discard by
returning nil. For example select even numbers

* (mapcan (lambda(n)(when (evenp n) (list n)))
'(1 2 3 4 5))
=> (2 4)

Using ,@ instead of , permits the use of this idiom.

Alan Crowe
Edinburgh
Scotland
.