The best way to learn the Racket Macro system for true style programmers

What is the best route for an experienced imperative style programmer familiar with C macros to learn about the Racket macro system. Not only its mechanics (how?), But also where and why to use it, and examples illustrating this.

Should I first study the Scheme (or Lisp) macros? I heard that the book "On Lisp" has a good explanation of Lisp macros with great examples of their use. Will it be useful or not?

+7
source share
2 answers

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.

+6
source

I believe Kent Dybvig's "Writing Hygenic Macros in a Schema with syntax-case " Still Remains the Best Macro Tutorial . This is not specific to Racket, but basically everything will be carried forward, and after reading the guide , Chris mentioned above will cover any remaining differences. Despite its name, it covers both "hygiene" and "non-hygiene" macros.

Edit July 2014:

As I wrote above, Greg Hendershott wrote an awesome macro tutorial on Racket, entitled Fear of Macros . This is now the best place to start exploring Racket macros.

+12
source

All Articles