Python syntax in Lisp and Scheme

David Eppstein eppstein at ics.uci.edu
Mon Oct 6 13:46:13 EDT 2003


In article 
<my-first-name.my-last-name-0610030955090001 at k-137-79-50-101.jpl.nasa.go
v>,
 my-first-name.my-last-name at jpl.nasa.gov (Erann Gat) wrote:

> > : Here's another example of what you can do with macros in Lisp:
> > 
> > : (with-collector collect
> > :   (do-file-lines (l some-file-name)
> > :     (if (some-property l) (collect l))))
> > 
> > : This returns a list of all the lines in a file that have some property. 
> > 
> > OK, that's _definitely_ just a filter: filter someproperty somefilename
> > Perhaps throw in a fold if you are trying to abstract "collect".
> 
> The net effect is a filter, but again, you need to stop thinking about the
> "what" and start thinking about the "how", otherwise, as I said, there's
> no reason to use anything other than machine language.

Answer 1: literal translation into Python.  The closest analogue of 
with-collector etc would be Python's simple generators (yield keyword) 
and do-with-file-lines is expressed in python with a for loop.  So:

def lines_with_some_property(some_file_name):
    for l in some_file_name:
        if some_property(l):
            yield l

Your only use of macros in this example is to handle the with-collector 
syntax, which is handled in a clean macro-free way by Python's "yield".
So this is unconvincing as a demonstration of why macros are a necessary 
part of a good programming language.

Of course, with-collector could be embedded in a larger piece of code, 
while using yield forces lines_with_some_property to be a separate 
function, but modularity is good...

Answer 2: poetic translation into Python.  If I think about "how" I want 
to express this sort of filtering, I end up with something much less 
like the imperative-style code above and much more like:

[l for l in some_file_name if some_property(l)]

I have no problem with the assertion that macros are an important part 
of Lisp, but you seem to be arguing more generally that the lack of 
macros makes other languages like Python inferior because the literal 
translations of certain macro-based code are impossible or more 
cumbersome.  For the present example, even that argument fails, but more 
generally you'll have to also convince me that even a freer poetic 
translation doesn't work.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list