What's better about Ruby than Python?

Kenny Tilton ktilton at nyc.rr.com
Wed Aug 20 19:51:05 EDT 2003


Chris Reedy wrote:
> Doug Tolton wrote:
> 
>> On Tue, 19 Aug 2003 13:43:11 +0200, Alex Martelli <aleaxit at yahoo.com>
>> wrote:
>>
>>> Doug Tolton wrote:
>>>  ...
>>>
>>>> abstractions and better programmer productivity.  Why not add Macro's
>>>> allowing those of us who know how to use them and like them to use
>>>> them.  If you hate macros, or you think they are too slow, just don't
>>>> use them.
>>>


....


> I'm curious. Why do you feel such a need for macros? With metaclasses, 
> etc., etc., what significant advantage would macros buy you? Do you have 
> any examples where you think you could make a significantly crisper and 
> easier to read and understand program with macros than without.

This macro:

(defmacro c? (&body code)
   `(let ((cache :unbound))
      (lambda (self)
        (declare (ignorable self))
        (if (eq cache :unbound)
	   (setf cache (progn , at code))
	 cache))))

Let's me write this (for some slot of an instance):

  (c? (+ 10 (left self))) ;; self ala smalltalk

Instead of this:

  (let ((cache :unbound))
   (lambda (self)
     (declare (ignorable self))
     (if (eq cache :unbound)
         (setf cache (+ 10 (left self)))
       cache)))

The above macro is a toy version of the real thing, which expands to this:

  (make-c-dependent
   :code '((+ 10 (left self)))
   :rule (lambda (c &aux (self (c-model c)))
           (declare (ignorable self c))
           (+ 10 (left self))))

Clearly (c? (+ 10 (left self))) is more readable; all the dataflow 
wiring is hidden. And the application is more maintainable should I 
decide to change the implementation of my dataflow hack.

This kinda thing is when Lispniks use macros, to silently wrap code with 
infrastructure necessary to satisfy some frequently arising requirement.

Especially cool above is that I capture the code in symbolic form in a 
separate slot for debugging purposes, as well as hand it to the compiler 
as the body of the lambda function. Took me way too long to think of 
that when I had no idea what lambda was backtracing. But as soon as I 
changed the macro, every (c? ) form (and there are hundreds) was debuggable.

I think macros are no harder to learn than an API, and most Lispniks 
won't stray to any language that lacks procedural macros (ie, they are 
useful), so maybe it comes down to what someone else said in this 
thread: Python is not trying to be everything. Fair enough. Let Python 
be Python, let Lisp be Lisp.

ie, If someone wants macros, they probably would also like special 
variables and closures and lexical scope and multi-methods and they may 
as well get it over with and learn Lisp and stop trying make Python more 
than it wants to be.


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Career highlights? I had two. I got an intentional walk from
Sandy Koufax and I got out of a rundown against the Mets."
                                                  -- Bob Uecker





More information about the Python-list mailing list