学习计划宏。帮我写一个define-syntax-rule

时间:2009-10-01 00:05:50

标签: functional-programming macros scheme

我是Scheme Macros的新手。如果我只有一个模式,并且我想结合define-syntax和syntax-rules,我该怎么做?

(define-syntax for
  (syntax-rules (from to)
    [(for i from x to y step body) ...]
    [(for i from x to y body) ...]))

如果我只有一个,我如何组合语法定义和规则?

感谢。

1 个答案:

答案 0 :(得分:4)

换句话说,您认为for实际上只需要一种模式,并希望编写如下内容:

(defmacro (for ,i from ,x to ,y step ,body)
  ; code goes here
  )

Scheme中没有任何内置功能可以让单模式宏更快地写入。传统的解决方案是(惊喜!)编写另一个宏。

我使用了defsubst from Swindle和PLT Scheme now ships with define-syntax-rule来做同样的事情。如果您正在学习宏,那么编写自己的define-syntax-rule等效项将是一个很好的练习,特别是如果您想要某种方式来表示“for”和“from”这样的关键字。 defsubstdefine-syntax-rule都不会处理这些问题。