Scheme programs can define and use new derived expression types, called macros. Program-defined expression types have the syntax

({keyword} {datum} ...)

where keyword is an identifier that uniquely determines the expression type. This identifier is called the syntactic keyword, or simply keyword, of the macro. The number of the datums, and their syntax, depends on the expression type.

Each instance of a macro is called a use of the macro. The set of rules that specifies how a use of a macro is transcribed into a more primitive expression is called the transformer of the macro.

The macro definition facility consists of two parts:

  • A set of expressions used to establish that certain identifiers are macro keywords, associate them with macro transformers, and control the scope within which a macro is defined, and
  • a pattern language for specifying macro transformers.

The syntactic keyword of a macro can shadow variable bindings, and local variable bindings can shadow syntactic bindings. Two mechanisms are provided to prevent unintended conflicts:

  • If a macro transformer inserts a binding for an identifier (variable or keyword), the identifier will in effect be renamed throughout its scope to avoid conflicts with other identifiers. Note that a global variable definition may or may not introduce a binding; see section 5.3.
  • If a macro transformer inserts a free reference to an identifier, the reference refers to the binding that was visible where the transformer was specified, regardless of any local bindings that surround the use of the macro.

In consequence, all macros defined using the pattern language are "hygienic" and "referentially transparent" and thus preserve Scheme's lexical scoping. [21,22,2,9,12]

Note Implementations may provide macro facilities of other types. Husk also provides explicit renaming macros that are compatible with hygienic macros but that also provide a low-level facility that can break hygiene if desired.

husk-scheme online documentation rev 3.2 (2021.03.04)