Python syntax in Lisp and Scheme
Erann Gat
my-first-name.my-last-name at jpl.nasa.gov
Mon Oct 6 15:19:54 EDT 2003
In article <eppstein-9700A3.10461306102003 at news.service.uci.edu>, 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
You left out the with-collector part.
But it's true that my examples are less convincing given the existence of
yield (which I had forgotten about). But the point is that in pre-yield
Python you were stuck until the langauge designers got around to adding
it.
I'll try to come up with a more convincing short example if I find some
free time today.
E.
More information about the Python-list
mailing list