Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 691
unj2 Avatar asked Apr 13 '26 03:04

unj2


1 Answers

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.

like image 95
Nathan Shively-Sanders Avatar answered Apr 16 '26 10:04

Nathan Shively-Sanders



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!