What can you do in LISP that you can't do in Python

Darren New dnew at san.rr.com
Tue May 15 17:35:20 EDT 2001


Thomas Bellman wrote:
> Assume that you have a bunch of things to do.  Before each
> operation, you want to call spam(), and after the operation, you
> want to call eggs().  I.e the equivalent of
> 
>     def spam_and_eggs(code):
>         spam()
>         exec code
>         eggs()
> 
> In LISP:
> 
>     (defmacro spam-and-eggs (&rest code)
>       `(progn
>          (spam)
>          , at code
>          (eggs)))
> 
> Now, you have fifty different things that you want to do, each
> thing "protected" with spam-and-eggs, and they should be done in
> a row:

In Tcl, something like

proc spam_and_eggs {thinglist} {
  foreach code $thinglist {
    spam
    uplevel -1 exec $code
    eggs
  }
}

# Note that "uplevel" is there to keep the things in thinglist
# from seeing "code" as a local variable. I might have the syntax
# on the uplevel call wrong.

spam_and_eggs {
  {this thing
   that thing}
  {The second thing
   second half}
  }

This would run
spam
this thing
that thing
eggs
spam
The second thing
second half
eggs

If you want to parse Tcl code, it's very easy. There are routines to
parse strings into individual commands. 

Other fun Tcl stuff:

proc xxx {name parms body} {
  origproc $name $parms "start $name\n$body\nend $name"
}

rename proc origproc ; rename xxx proc

Now everything after that calls "start" at the start of each body and
"end" at the end (assuming no "return" in there).

You can also do things like iterating thru all the functions that start
with "impl_*" and parsing the argument lists to generate a usage message
automatically. You can iterate through a namespace to find all the
functions declared there and delegate from your namespace to that other
namespace, which is handy when doing layered network protocol design.
Etc.

Not noticably harder in Tcl than Lisp. 

> In LISP, the macro basically gets passed a parse tree of the
> code. 

Since Tcl is linear, there's no need to pass a parse tree. I.e., since
code and data are the same thing, you don't need to pre-parse it. It's
all strings.

> (Or of whatever kind of data you give it as an argument;
> LISP doesn't distinguish between code and data the way many other
> languages do.)

Neither does Tcl, often to the surprise of people expecting comments to
still be comments inside of strings that look like code.

-- 
Darren New / Senior MTS & Free Radical / Invisible Worlds Inc.
       San Diego, CA, USA (PST).  Cryptokeys on demand.
     This is top-quality raw fish, the Rolls-Rice of Sushi!



More information about the Python-list mailing list