学习方案宏。帮我写一个定义语法规则
我是计划宏的新手。如果我只有一种模式,并且想结合定义语法和语法规则,我该怎么做?
(define-syntax for
(syntax-rules (from to)
[(for i from x to y step body) ...]
[(for i from x to y body) ...]))
如果我只有一个,如何将语法定义和规则结合起来?
谢谢。
I am new to Scheme Macros. If I just have one pattern and I want to combine the define-syntax and syntax-rules, how do I do that?
(define-syntax for
(syntax-rules (from to)
[(for i from x to y step body) ...]
[(for i from x to y body) ...]))
If I just have one for, how do I combine the syntax definition and the rule?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
换句话说,您认为
for
实际上只需要一种模式,并且想要编写类似以下内容的内容:Scheme 中没有内置任何内容可以使单模式宏的编写速度更快。传统的解决方案是(惊讶!)编写另一个宏。
我使用了 Swindle 的
defsubst
和 PLT 方案 < a href="http://docs.plt-scheme.org/guide/pattern-macros.html" rel="nofollow noreferrer">现在附带define-syntax-rule
它做同样的事情。如果您正在学习宏,那么编写您自己的define-syntax-rule
等效项将是一个很好的练习,特别是如果您想要某种方式来指示“for”和“from”等关键字。defsubst
和define-syntax-rule
都不能处理这些问题。In other words, you decided that
for
really only needs one pattern and want to write something like:There is nothing built-in to Scheme that makes single-pattern macros faster to write. The traditional solution is (surprise!) to write another macro.
I have used
defsubst
from Swindle, and PLT Scheme now ships withdefine-syntax-rule
which does the same thing. If you are learning macros, then writing your owndefine-syntax-rule
equivalent would be a good exercise, particularly if you want some way to indicate keywords like "for" and "from". Neitherdefsubst
nordefine-syntax-rule
handle those.