Problem with macro behaviour in lisp -
if in repl this:
(dolist (x (1 2 3)) (print x))
then error since in (1 2 3) digit 1 not symbol or lambda expr. if do:
(dolist (x (list 1 2 3)) (print x))
then works ok.
my question why following works:
repl> (defmacro test (lst) (dolist (x lst) (print x))) => test repl> (test (1 2 3)) 1 2 3 =>nil
why dolist accept (1 2 3) when inside macro definition not when directly in repl? assumption:
"since test macro ,it not evaluate arguments, (1 2 3) passed dolist macro. dolist must complain when passed (1 2 3) in repl"
is wrong. where?
update: although answers clarify misunderstandings macros, question still stands , try explain why:
we have established dolist evaluates list argument(code blocks 1, 2). well, doesnt seem case when called inside macro definition , list argument passed 1 of defined macro arguments(code block 3). more details: macro, when called, not evaluate arguments. test macro, when called, preserve list argument , pass dolist @ expansion time. @ expansion time dolist executed (no backquotes in test macro def). , executed (1 2 3) argument since test macro call passed it. why doesnt throw error since dolist tries evaluate list argument, , in case list argument (1 2 3) not evaluatable. hope clears confusion bit.
macros arguments passed unevaluated. may choose evaluate them. dolist
list argument. works unquoted list passed in lst
in macro test
:
(defmacro test (lst) (dolist (x lst) (print x)))
that's because @ macro-expansion time dolist
sees lst
argument. when evaluates it, gets list (1 2 3)
.
Comments
Post a Comment