In this post I want to go a little deeper into the macro system of Scheme (particularly Chicken), and share a couple of macros I've created.
First of all, macros are what make Scheme and Lisp so exciting. Macros offer the ability to evaluate code at compile time, and modify the structure of a program before it even runs. You can think of it as a really really powerful
#define, but where #define lets you modify text, a Lisp macro lets you modify the code structure itself.
There are a couple of different kinds of macro available in Scheme, and as I'm somewhat of a young schemer I'm not completely sure which are available across all implementations and which are only available in Chicken. But I will assume that the macros here can be ported with relative ease.
The first kind, looks a lot like a regular ol' function. It is even called like a function, only when this macro is read it leaves behind whatever it returns. Here's an annotated example:
; Here we are going to create a new macro called "return-me" ; that will simply return in a list whatever is passed to it (define-syntax return-me (syntax-rules () ; Any symbols placed in this list will be ignored [(return-me body ...) ; This is what the arguments should look like (list body ...)])) ; And this is how they should be transformed ; Example (return-me "Hello" "World") => ("Hello" "World")