Macros
a way to combine unevaluated input expressions into another expression
Expressions as data
Python
programs are composed of expressions, but manipulate values or data
Scheme
expresions are either primitive expressions or lists, which means they're also a form of data, this means:
we can assign expressions to variables
pass expressions into functions
create and return new expressions within functions
define-macro
special form - creates a macro procedure
Macros take in and return expressions, which are then evaluated in place of the call to the macro
Evaluating Macros
Evaluating regular call expressions:
Evaluate the operator sub-expression, which evaluates to a regular procedure
Evaluate the operand expressions in order
Apply the procedure to the evaluated operands
Evaluating Macros:
Evaluate the operator sub-expression, which evaluates to a macro procedure
Apply the macro procedure to the operand expressions without evaluating them
Evaluate the expression returned by the macro procedure in the frame the macro was called in
Writing Macros
because macros take in and return expressions, when writing macros, you should think about:
what types of expressions you'll take in
what expression has equivalent behavior to your macro
Last updated