Can I make a macro that expands into more than one value?

霸气de小男生 提交于 2019-12-08 01:19:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!