Re: macro question
- From: Alan Crowe <alan@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: 30 Sep 2005 18:06:14 +0100
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
.
- References:
- macro question
- From: Karol Skocik
- macro question
- Prev by Date: Re: macro question
- Next by Date: Re: Why Bother Trolling?
- Previous by thread: Re: macro question
- Next by thread: Giving info to CONSTANTP
- Index(es):