The rocket documentation has an excellent macro tutorial .
I would definitely recommend Scheme macros over CL macros if you haven't done Lisp macros before. This is because Scheme macros use pattern matching, and it is much easier to read.
Example (using Racket define-syntax-rule ):
(define-syntax-rule (let ((var val) ...) expr ...) ((lambda (var ...) expr ...) val ...))
This is a very simple macro that defines let in terms of creating the corresponding lambda and then applying it. Easy to read and easy to talk about what he is doing.
Slightly more complex macro:
(define-syntax let* (syntax-rules () ((let* () expr ...) (let () expr ...)) ((let* ((var val) next ...) expr ...) (let ((var val)) (let* (next ...) expr ...)))))
This defines let* in terms of nested let s, so bindings are done sequentially. It includes a base register (without bindings), as well as a recursive case.
Chris jester-young
source share