common lisp - Additional symbol LIST when using ,@ -
i observed macro expansion not understand:
(defmacro test (cons-list) `(list ,@(mapcar #'(lambda(elem) elem) cons-list))) (defmacro test-2 () `(list ,@(list (cons "a" "b")))) (defmacro test-3 (cons-list) `(list ,@cons-list))
i'd expect both macros expand in same fashion, use mapcar in fancy way of creating same list again , use list. results observed in sbcl are:
(test (list (cons "a" "b")))
expands(list list (cons "a" "b"))
(test-2)
expands(list ("a" . "b"))
(test-3 (list (cons "a" "b")))
again expands(list list (cons "a" "b"))
why don't these macro expansions behave same?
test-2
evaluates form (list (cons "a" "b"))
, other 2 not.
remember: arguments macro forms read, unevaluated.
in order same behaviour test-2
, have quote form: ,@'(list (cons "a" "b"))
.
edit: here step-by-step expansion of test
:
`(list ,@(mapcar #'(lambda (elem) elem) cons-list))
removing backquote syntactic sugar:
(list* 'list (mapcar #'(lambda (elem) elem) cons-list)
argument substitution in example:
(list* 'list (mapcar #'(lambda (elem) elem) '(list (cons "a" "b")))
evaluate mapcar
form:
(list* 'list '(list (cons "a" "b")))
evaluate `list*' form:
'(list list (cons "a" "b"))
Comments
Post a Comment