Python syntax in Lisp and Scheme

Bengt Richter bokr at oz.net
Tue Oct 7 03:43:28 EDT 2003


On Mon, 06 Oct 2003 10:46:13 -0700, David Eppstein <eppstein at ics.uci.edu> wrote:

>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)]
>

Just thought of this: a generator list comprehension, so your could write

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

and get a generator that is the equivalent of your lines_with_some_property above.

I.e., in general you should be able to convert any list comprehension into a generator by
putting 'yield ' at the front. Has someone proposed this already? I seems a natural, unless
I am blindly optimistic, which is quite possible ;-)

>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.
>

Regards,
Bengt Richter




More information about the Python-list mailing list