Google Groups Home
Help | Sign in
Message from discussion Python syntax in Lisp and Scheme
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Jock Cooper  
View profile
 More options Oct 6 2003, 9:20 pm
Newsgroups: comp.lang.lisp, comp.lang.python, comp.lang.scheme, comp.lang.functional
From: Jock Cooper <jo...@mail.com>
Date: 06 Oct 2003 18:02:16 -0700
Local: Mon, Oct 6 2003 9:02 pm
Subject: Re: Python syntax in Lisp and Scheme

my-first-name.my-last-n...@jpl.nasa.gov (Erann Gat) writes:
> In article <eppstein-9700A3.10461306102...@news.service.uci.edu>, David
> Eppstein <eppst...@ics.uci.edu> wrote:

> > In article
> > <my-first-name.my-last-name-0610030955090...@k-137-79-50-101.jpl.nasa.go
> > v>,
> >  my-first-name.my-last-n...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google