shell
shell

Reputation: 1937

Do lisp macros ever evaluate their expressions, or do they just do transformation?

I am new to lisps and am coming from a c background. I am trying to understand lisp macros. I understand that they do an arbitrary transformation from one set of expressions to another set of expressions, but do they ever evaluate their expressions while expanding?

One could imagine a macro that recursively removes an item from the passed-in list until the list has no elements left. It would have to evaluate the list's size to know when to stop expanding. Is this sort of thing possible?

Upvotes: 0

Views: 79

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139411

CL-USER 11 > (defmacro consume-list (list)
              (if (null list)                      ; empty list?
                  ()                               
                (list 'consume-list (rest list)))) ; remove first element
CONSUME-LIST

CL-USER 12 > (macroexpand-1 '(consume-list (a b c)))  ; expand once
(CONSUME-LIST (B C))                        ; first element has been removed
T

CL-USER 13 > (macroexpand '(consume-list (a b c)))  ; expand to completion
NIL                                                 ; everything has been removed
T

Upvotes: 2

Related Questions