Python syntax in Lisp and Scheme

Jock Cooper jockc at mail.com
Mon Oct 6 21:02:16 EDT 2003


my-first-name.my-last-name at jpl.nasa.gov (Erann Gat) writes:

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

I'm afraid it's very hard to give any convincing examples of the
utility of macros -- as long as you are sticking to trivial examples.
On the other hand, you can't exactly post real world complex examples
of how macros saved you time and LOC (we all have em) because reader's
eyes would just glaze over.  I think macros are just another one of 
CL's features that some most people just don't get until they actually
use them.  But here's a small one:

I wrote about 60 lines worth of macro based code (including a few reader
 macros) that allows me to write things like:

(with-dbconnection
  (sql-loop-in-rows 
   "select col1, col2 from somewhere where something"
   :my-package row-var "pfx"
   ...
   ...some code...
   ...))

In the "some code" section, the result columns' values are accessed by
#!pfx-colname (eg #!pfx-col1), or directly from row-var using
#?pfx-colname (which returns the position).  Also, error handling code
can be automatically included by the macro code.  How much time and
effort (and possible bugs) has this saved me?  Well at least 60+ lines
or more of boilerplate every time I use this pattern..  Plus the expansions
for #!colname include error checks/warnings etc. -- all hidden from view.  

Jock
---
www.fractal-recursions.com










More information about the Python-list mailing list