问题
Is there a way to define a racket macro foo so that
(list 1 (foo 2 3) 4)
expands into
(list 1 2 3 4)
?
回答1:
As other answers have mentioned, you cannot have a macro expand into more than one value, and have that spliced into the calling context. But you can do something similar using quasiquotation.
Assuming your macro is adapted to return a list instead, you can do this (for your given example):
`(1 ,@(foo 2 3) 4)
Example (tested in Racket):
> `(1 ,@(map sqrt '(2 3)) 4)
'(1 1.4142135623730951 1.7320508075688772 4)
回答2:
It's currently not possible (and seems unlikely to change in the near future). Here's one thread discussing this. See in particular the answer by Matthew Flatt:
allowing splicing of results in function-call subexpressions would break equivalences that are currently exploited by macros and the compiler.
来源:https://stackoverflow.com/questions/19194884/can-i-make-a-macro-that-expands-into-more-than-one-value