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:

  1. Evaluate the operator sub-expression, which evaluates to a regular procedure

  2. Evaluate the operand expressions in order

  3. Apply the procedure to the evaluated operands

Evaluating Macros:

  1. Evaluate the operator sub-expression, which evaluates to a macro procedure

  2. Apply the macro procedure to the operand expressions without evaluating them

  3. 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:

  1. what types of expressions you'll take in

  2. what expression has equivalent behavior to your macro

Last updated