unj2
unj2

Reputation: 53481

Learning Scheme Macros. Help me write a define-syntax-rule

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.

Upvotes: 3

Views: 508

Answers (1)

Nathan Shively-Sanders
Nathan Shively-Sanders

Reputation: 18389

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.

Upvotes: 4

Related Questions