merits of Lisp vs Python
Kirk Sluder
kirk at nospam.jobsluder.net
Sun Dec 10 01:06:41 EST 2006
In some cases lisp macros are a way of saying tomato to something
that would be called "tomahto" in python.
One common use of macros is custom iteration constructs. In my
social network analysis I wanted to do something to each and every
sender-recipient pair in the header line of "mail" messages in my
dataset. (And yes, I did have permission to use this data.) Each
message had one sender, but possibly multiple recipients. Since I
would be doing multiple forms of analysis, I might as well create
one iterator to do this.
The DO-MAIL-RELATIONS macro takes the mail message (line), parses
the sender field and assigns it to (sender), splits the recipient
field and assigns it to (recip) and then performs the logic in the
body which often involved looking up values in a database, and
setting values in a matrix.
(kirk.matrix-process:do-mail-relations (message sender recip)
;;about 12 lines of logic snipped
)
To be pedantic about this call. "KIRK.MATRIX-PROCESS" identifies the
package/namespace for the macro call. "DO-MAIL-RELATIONS" names the
macro as part of the DO family of iteration constructs which are
basic to lisp.
Now that I think about this, in python I'd probably do this using
object logic that returned the recipient list as an iteratable
object:
for sender, recipient in message.relationPairs:
#about 12 lines of logic
Would it have been possible to implement DO-MAIL-RELATIONS as a
function? Possibly, but I found trying to compact all of that logic
into a single-use function that could be safely passed to another
function to be more trouble.
Another common macro use is the "WITH-STREAM" family which opens a
stream for IO and closes it at the end.
(with-open-file (file-handle "filename" :direction :output)
(format file-handle "Hello world.~%")
)
The pythonic way to do this would be to create a class that
implements file-like behaviors:
output = fileLike.open()
output.write("Hello world\n")
output.close()
You might want to use a custom "WITH-STREAM" macro or file-like
object if you need to format, filter, or compress the outgoing
stream in any way.
More information about the Python-list
mailing list