Question

Is it any analogies to Scheme define-syntax in ELisp? In define-syntax I can specify some keywords and in elisp defmacro seems I cant. EDIT: Seems I either do not understand you well, or my question is not clear. In scheme it is possible to define macro with reserved words like

(loop for a in some-list

(loop while

In Emacs Lisp I found not way to introduce such sugar. At most, I can make for macro, that must be called like

(loop-for a list (do-a lot of stuff…)

Readability matters

Was it helpful?

Solution

Both Scheme and Elisp have macro systems. The "keywords" define-syntax and defmacro both introduce a name for a user defined macro.

At the time a macro, foo, is used in actual code, say (foo 1 bar) the macro expander must determine how to rewrite the given form, (foo 1 bar), into a simpler form that do not contain user macros. The macro expander calls the function that was defined at the time the macro foo was defined. I.e. it calls the function you specified with define-syntax or defmacro with an representation of the form, (foo 1 bar). The representation can be as "syntax objects" or plain lists (this differs in different macro systems).

This is my take on the similarities between define-syntax and defmacro.

The macro systems of R5RS Scheme and Elisp are different though.

The macro expander associated with foo can in R5RS Scheme be specified with the help of syntax-rules. This allows you to use pattern matching to specify the rewrite rules (internally the syntax-rules form will evaluate to function). Other differences: The macro expansion algorithm will in R5RS Scheme help you agains inadvertently introduce names clashing with names from other parts of your program (say from a library, you did not write your self). Historically Due to the way namespaces works in Lisp, this problem is not that big a deal in Lisp, but it is possible to make mistakes.

Historically Scheme and Elisp both used the same macro expansion algorithm, but then Schemers started experimenting with other algorithms. The "syntax-rules" systems was introduced in R5RS, but the evolution didn't stop there. These days all modern Scheme implementation have (variants of) the "syntax-case" system.

Some implementation have done work to make this expansion algorithm work with modules (Racket, R6RS-implementations, and others).

In short when you read about Scheme macro systems, take care to examine precisely which variant you are reading about. If you are reading about restrictions, then most likely you have found a text on the (now rather old) syntax-rules system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top