What can you do in LISP that you can't do in Python
Nonexistence
huaiyuan at rac3.wam.umd.edu
Tue May 15 23:43:53 EDT 2001
"Terry Reedy" <reedy37 at home.com> writes:
> "Nonexistence" <huaiyuan at rac3.wam.umd.edu> wrote in message
> news:c6x1yprz2u4.fsf at rac3.wam.umd.edu...
>
> ...
> > (DEFMACRO NEW-STATE (TAG)
> > `(PROGN (IF TRACING
> > (PUSH (CONS STATE (COPY-LIST REGISTERS))
> > STATE-HISTORY))
> > (GO ,TAG)))
> >
> ...
>
> What I know about Lisp macros is what I have gleaned from this thread,
> which isn't too much yet. How is the above different from writing and
> calling a function. IE
>
> def new_state(tag):
> if tracing: store_tracing_info()
> goto(tag)
>
> new_state(tag)
(Disclaimer: I am not the original author of the example, so I may be
misinterpretating his intention.)
Notice the variables TRACING, STATE, REGISTERS, and STATE-HISTORY above;
they're not global variables, but "captured" local variables. If you have
multiple instances of state machine (each properly setup in its own lexical
environment), the macro version of NEW-STATE above won't have the problem
of clobbering variables; i.e., each instance can have its own version of
STATE-HISTORY.
Variable capturing is usually to be avoided when creating macro; it
violates referential transparency. But I guess it is justifiable here
because the above is supposed to be internal debugging code.
- huaiyuan
More information about the Python-list
mailing list