Does someone know why the following produces the expected result – (2 4 6)
(defmacro mult2 (lst)
(define (itter x)
(list '* 2 x))
`(list ,@(map itter lst)))
(mult2 (1 2 3))
while I expected that this one would (with the list identifier)
(defmacro mult2 (lst)
(define (itter x)
(list '* 2 x))
`(list ,@(map itter lst)))
(mult2 '(1 2 3))
That’s because the
'(1 2 3)is expanded by the reader into(quote (1 2 3)). Since you only destructure one list in your macro, it won’t work as expected.Some general advice: if you’re working in Racket you probably want to avoid using
defmacro. That is definitely not the idiomatic way to write macros. Take a look at syntax-rules and, if you want to define more complicated macros, syntax-parse. Eli also wrote an article explaining syntax-case for people used to defmacro.