Macros are not covered in SICP. I didn’t know how to use macro in scheme until I read Peter Norvig’s JScheme code. Only then I started to understand why “symbol” and “quote” are important to scheme. Scheme macros is similar to C macro, both transform some pattern into another. Scheme macros are more powerful because expansion/transformation happens at run time, and input/output patterns are structured scheme data that represents scheme code.
Let’s look at an example. We know that let-expression can be replaced by equivalent lambda expression application.
(let ((x 1)(y 2)) (+ x y))
That transformation can be easily defined as a macro.
What the code does, is basically transform input like
(my-let ((x 1)(y 2)) (+ x y))into
((lambda (x y) (+ x y)) 1 2). Macro is very similar to functions, except that it’s parameter is quoted code, and it returns quoted code as well. When scheme interpreter sees an macro application, it will
- expand macro by applying macro body with parameter bound to quoted parameter expressions (without evaluating them).
- evaluate result of macro expansion.
One thing I like about macro is it makes scheme interpreter/compiler much easier to implement. To write a scheme interpreter, you only need to implement very few core features (begin, define, lambda/macro, function call, quote, if expression, built-in functions). The rest of the language like let, cond, and, or can all be implemented in macros. JScheme source code uses this approach, and its source code contains a file that defines all these language features with macros. These definitions are reusable when writing scheme interpreter/compiler in the future. It is amazing that that adding a language feature can make the language easier to implement.
Quasiquote is the same as quote except that you can “unquote” part of the expression.
(quote (1 (+ 2 3)))
Unquoted code is evaluated instead of being treated as data. The best way to explain unquote-splicing is by example.
(quasiquote (1 (unquote-splicing (append (list 2) (list 3))) 4 ))
These are utilities that make macro much easier to write. “my-let” above can be written as
It might be brain twisting when looking at these for the first time, but it will start to make more sense when started writing more macros. Surprisingly, quasiquote, unquote, unquote-splicing can be implemented with macros. Following implementation is written by Darius Bacon, and I copied it from JScheme code.
To be honest I still don’t full understand the code, but I did some tests and was convinced it works.
Macros are powerful, but there is an issue when used in practice. Consider following example
User code might modify variables defined in macro body and cause unexpected behavior. Hygiene macro addresses this problem by replacing variables defined in macro body with unique identifiers dynamically so that user don’t have to worry about naming collision. R5RS scheme has syntax-rules syntax which supports hygiene macro and pattern matching features. There are many resources on web about syntax-rule like this, and I still don’t understand it’s pattern matching mechanism well. But surprisingly again, syntax-rules can be implemented with regular macro as well! I found this implementation in JScheme home page.
It is possible to extend scheme language feature with macro. One example is stream (infinite length list) processing. Scheme does not natively support stream processing, but it is easy to add steaming support using macro.
(define stream-cons (macro (a b) `(cons ,a (lambda () ,b))))
Here is a program that creates infinite stream of prime numbers using Sieve of Eratosthenes.
(define (stream-map f s)
Macro is such a simple yet powerful feature that makes scheme a beautiful languages. I am still learning and being surprised by what macro can do.