Optional 2: define-macro
Macros allow the language itself to be extended by the user. Simple macros can be provided with the define-macro
special form. This must be used like a procedure definition, and it creates a procedure just like define
. However, this procedure has a special evaluation rule: it is applied to its arguments without first evaluating them. Then the result of this application is evaluated.
This final evaluation step takes place in the caller's frame, as if the return value from the macro was literally pasted into the code in place of the macro.
Here is a simple example:
scm> (define (map f lst) (if (null? lst) nil (cons (f (car lst)) (map f (cdr lst)))))
scm> (define-macro (for formal iterable body)
.... (list 'map (list 'lambda (list formal) body) iterable))
scm> (for i '(1 2 3)
.... (print (* i i)))
1
4
9
(None None None)
The code above defines a macro for
that acts as a map
except that it doesn't need a lambda around the body.
In order to implement define-macro
, complete the implementation for do_define_macro
, which should create a MacroProcedure
and bind it to the given name as in do_define_form
. Then, update scheme_eval
so that calls to macro procedures are evaluated correctly.
Use Ok to test your code:
python ok -q optional_2