Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro-defining macro in Racket?

In Common Lisp it is relatively easy to create a macro-defining macro. For example, the following macro

(defmacro abbrev (short long)
  `(defmacro ,short (&rest args)
     `(,',long ,@args)))

is a macro-defining macro, because it expands to another macro.

If we now put

(abbrev def defun) 

in our program, we can write def instead of defun whenever we define a new function. Of course, abbrev can be used for other things, too. For example, after

(abbrev /. lambda)

we can write (/. (x) (+ x 1)) instead of (lambda (x) (+ x 1)). Nice. (For detailed explanation of abbrev, see http://dunsmor.com/lisp/onlisp/onlisp_20.html)

Now, my questions are:

  1. Can I write the macro-defining macros in Racket?
  2. If I can, how to do that? (for example, how to write something similar to abbrev macro in Racket?)
like image 279
Racket Noob Avatar asked Oct 27 '14 21:10

Racket Noob


1 Answers

According to this part of the Racket Guide:

(define-syntax-rule (abbrev short long)
  (define-syntax-rule (short body (... ...))
    (long body (... ...))))

Quoting the above link:

The only non-obvious part of its definition is the (... ...), which “quotes” ... so that it takes its usual role in the generated macro, instead of the generating macro.

Now

(abbrev def define)
(abbrev /. lambda) 
(def f (/. (x) (+ x 1)))
(f 3)  

yields

4

FWIW, it works on Guile as well, so it's no Racket-specific thing.

like image 58
uselpa Avatar answered Sep 20 '22 16:09

uselpa