Python syntax in Lisp and Scheme

Erann Gat my-first-name.my-last-name at jpl.nasa.gov
Mon Oct 6 12:55:09 EDT 2003


In article <blr1cq$bb1$1 at enyo.uwa.edu.au>, gregm at cs.uwa.edu.au wrote:

> In comp.lang.functional Erann Gat
<my-first-name.my-last-name at jpl.nasa.gov> wrote:
> :> I can't see why a LISP programmer would even want to write a macro.
> : That's because you are approaching this with a fundamentally flawed
> : assumption.  Macros are mainly not used to make the syntax prettier
> : (though they can be used for that).  They are mainly used to add features
> : to the language that cannot be added as functions.
> 
> Really? Turing-completeness and all that... I presume you mean "cannot
> so easily be added as functions", but even that would surprise me.

No, I meant what I wrote.  Turing-completeness is a red herring with
respect to a discussion of programming language features.  If it were not
then there would be no reason to program in anything other than machine
language.


> : For example, imagine you want to be able to traverse a binary tree and do
> : an operation on all of its leaves.  In Lisp you can write a macro that
> : lets you write:
> : (doleaves (leaf tree) ...)
> : You can't do that in Python (or any other langauge).
> 
> My Lisp isn't good enough to answer this question from your code,
> but isn't that equivalent to the Haskell snippet: (I'm sure
> someone here is handy in both languages)
> 
> doleaves f (Leaf x)     = Leaf (f x)
> doleaves f (Branch l r) = Branch (doleaves f l) (doleaves f r)
> 
> I'd be surprised if Python couldn't do the above, so maybe doleaves
> is doing something more complex than it looks to me to be doing.

You need to change your mode of thinking.  It is not that other languages
cannot do what doleaves does.  It is that other langauges cannot do what
doleaves does in the way that doleaves does it, specifically allowing you
to put the code of the body in-line rather than forcing you to construct a
function.

Keep in mind also that this is just a trivial example.  More sophisticated
examples don't fit well in newsgroup postings.  Come see my ILC talk for
an example of what you can do with macros in Lisp that you will find more
convincing.

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

> : DO-FILE-LINES and WITH-COLLECTOR are macros, and they can't be implemented
> : any other way because they take variable names and code as arguments.
> 
> What does it mean to take a variable-name as an argument? How is that
> different to taking a pointer? What does it mean to take "code" as an
> argument? Is that different to taking a function as an argument?

These questions are answered in various books.  Go seek them out and read
them.  Paul Graham's "On Lisp" is a good place to start.

E.




More information about the Python-list mailing list