我可以制作一个扩展为多个值的宏吗? [英] Can I make a macro that expands into more than one value?

查看:89
本文介绍了我可以制作一个扩展为多个值的宏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来定义球拍宏foo,这样

Is there a way to define a racket macro foo so that

(list 1 (foo 2 3) 4)

扩展为

(list 1 2 3 4)

?

推荐答案

如其他答案所述,您不能将宏扩展为多个值,而不能将其拼接到调用上下文中.但是您可以使用准引号进行类似的操作.

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)

这篇关于我可以制作一个扩展为多个值的宏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆