views:

159

answers:

1

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.

+4  A: 

In other words, you decided that for really only needs one pattern and want to write something like:

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

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 with define-syntax-rule which does the same thing. If you are learning macros, then writing your own define-syntax-rule equivalent would be a good exercise, particularly if you want some way to indicate keywords like "for" and "from". Neither defsubst nor define-syntax-rule handle those.

Nathan Sanders
This is all correct, and I can also add that writing such a `define-synatx-rule` macro is a very simple case of macro-that-generates-macros. But just in case you missed it, PLT *does* have a `define-syntax-rule` as part of the language.(BTW, those "noise words" are called "keywords", and you're correct that the common definition for `define-syntax-rule` doesn't have a place for them -- this is fine, since keywords are usually used to distinguish multiple patterns.)
Eli Barzilay
It's probably new since I last used PLT. Thanks for the terminology; I don't use keywords so I didn't know what they're called.
Nathan Sanders