Re: macro question
- From: Peter Seibel <peter@xxxxxxxxxxxxxxx>
- Date: Fri, 30 Sep 2005 16:49:10 GMT
"Karol Skocik" <Karol.Skocik@xxxxxxxxx> writes:
> Hello, I need help with macro style. I sometimes need a macro which
> outputs some code, but depending on some variable, I need to add
> something to the output or nothing, to be more specific :
>
> 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 '()))
> ...
>
> until now, I have found out that I can only make it happen when I make
> the decision before defining `(let ..) and then I need to duplicate
> code like this :
>
> (if make-undo
> `(let ((added-nodes (make-hash-table))
> (undo-funcs '())) ...
> `(let ((added-nodes (make-hash-table))) ...
>
> and that is ugly. or maybe make a list like 'result-form' and append
> stuff to that?
> I just can't use ,(when make-undo `(undo-funcs '())) because it leaves
> nil in the list when the condition is not true.
>
> How do you usually solve situations like this?
> Or did I miss something in basic macro writing rules?
You can do this:
`(let ((added-nodes (make-hash-table))
,@(when make-undo `((undo-funcs '()))))
...)
Note the extra ()'s around the undo-funcs--they'll get taken of by the
splicing of ,@ which will also strip the NIL if make-undo is false.
-Peter
--
Peter Seibel * peter@xxxxxxxxxxxxxxx
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp * http://www.gigamonkeys.com/book/
.
- Follow-Ups:
- Re: macro question
- From: Karol Skocik
- Re: macro question
- References:
- macro question
- From: Karol Skocik
- macro question
- Prev by Date: Re: C++ and lisp and Games
- Next by Date: Re: macro question
- Previous by thread: Re: macro question
- Next by thread: Re: macro question
- Index(es):