Python syntax in Lisp and Scheme

Pascal Costanza costanza at web.de
Mon Oct 6 14:20:28 EDT 2003


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

I don't know a lot about Python, so here is a question. Is something 
along the following lines possible in Python?

(with-collectors (collect-pos collect-neg)
   (do-file-lines (l some-file-name)
     (if (some-property l)
       (collect-pos l)
       (collect-neg l))))


I actually needed something like this in some of my code...

Pascal





More information about the Python-list mailing list