1 mzlib/ a-signature
2 mzlib/ a-unit
3 mzlib/ async-channel
4 mzlib/ awk
5 mzlib/ class
6 mzlib/ class100
7 mzlib/ cm
8 mzlib/ cm-accomplice
9 mzlib/ cmdline
10 mzlib/ cml
11 mzlib/ compat
12 mzlib/ compile
13 mzlib/ contract
14 mzlib/ control
15 mzlib/ date
16 mzlib/ deflate
17 mzlib/ defmacro
18 mzlib/ etc
19 mzlib/ file
20 mzlib/ for
21 mzlib/ foreign
22 mzlib/ include
23 mzlib/ inflate
24 mzlib/ integer-set
25 mzlib/ kw
26 mzlib/ list
27 mzlib/ match
28 mzlib/ math
29 mzlib/ md5
30 mzlib/ os
31 mzlib/ pconvert
32 mzlib/ pconvert-prop
33 mzlib/ plt-match
34 mzlib/ port
35 mzlib/ pregexp
36 mzlib/ pretty
37 mzlib/ process
38 mzlib/ restart
39 mzlib/ runtime-path
40 mzlib/ sandbox
41 mzlib/ sendevent
42 mzlib/ serialize
43 mzlib/ shared
44 mzlib/ string
45 mzlib/ struct
46 mzlib/ stxparam
47 mzlib/ surrogate
48 mzlib/ tar
49 mzlib/ thread
50 mzlib/ trace
51 mzlib/ traceld
52 mzlib/ trait
53 mzlib/ transcr
54 mzlib/ unit
55 mzlib/ unit-exptime
56 mzlib/ unit200
57 mzlib/ unitsig200
58 mzlib/ zip
Bibliography
Index
On this page:
define-macro
defmacro
Version: 4.0.2

 

 (require mzlib/defmacro)

(define-macro id expr)

(define-macro (id . formals) body ...+)

(defmacro id formals body ...+)

 

formals

 

=

 

(id ...)

 

 

|

 

id

 

 

|

 

(id ...+ . id)

Defines a (non-hygienic) macro id through a procedure that manipulates S-expressions, as opposed to syntax objects.

In the first form, expr must produce a procedure. In the second form, formals determines the formal arguments of the procedure, as in lambda, and the exprs are the procedure body. The last form, with defmacro, is like the second form, but with slightly different parentheses.

In all cases, the procedure is generated in the transformer environment, not the normal environment.

In a use of the macro,

  (id datum ...)

syntax->datum is applied to the expression, and the transformer procedure is applied to the cdr of the resulting list. If the number of datums does not match the procedure’s arity, or if id is used in a context that does not match the above pattern, then a syntax error is reported.

After the macro procedure returns, the result is compared to the procedure’s arguments. For each value that appears exactly once within the arguments (or, more precisely, within the S-expression derived from the original source syntax), if the same value appears in the result, it is replaced with a syntax object from the original expression. This heuristic substitution preserves source location information in many cases, despite the macro procedure’s operation on raw S-expressions.

After substituting syntax objects for preserved values, the entire macro result is converted to syntax with datum->syntax. The original expression supplies the lexical context and source location for converted elements.

Important: Although define-macro is non-hygienic, it is still restricted by PLT Scheme’s phase separation rules. This means that a macro cannot access run-time bindings, because it is executed in the syntax-expansion phase. Translating code that involves define-macro or defmacro from an implementation without this restriction usually implies separating macro related functionality into a begin-for-syntax or a module (that will be imported with require-for-syntax) and properly distinguishing syntactic information from run-time information.