Re: merits of Lisp vs Python

2006-12-12 Thread hit_the_lights

Neil Cerutti schrieb:

> On 2006-12-12, André Thieme <[EMAIL PROTECTED]> wrote:
> >> Contrast the much more common
> >>
> >>   a[i] = b[n]
> >>
> >> with
> >>
> >>   (setf (aref a i) (aref b n))
> >>
> >> and the attractions of Python may make more sense.
> >
> > Here Python and Lisp are equal, 7 tokens vs 7 tokens, but in
> > Python one has to write less since "[]" are 2 chars while
> > "aref" are 4, plus the setf.  But from counting the brain units
> > which I regard as an important factor they are both equal.
>
> A comparison of brain units of the above snippets is irrelevant,
> since the snippets are not equivalent.
>
> The Python snippet will work for any object a that provides
> __setitem__ and any object b that provides __getitem__.
>
> I don't know what an equivalent Lisp snippet would be (or even
> exactly how close the above snippet comes to matching the Python
> code), but whatever it is would be a better foundation for
> comparing brain units with the above Python.

It would be exactly like the example given, just "aref" replaced with
something else. An example implementation:

==
(defgeneric $ (container key))
(defgeneric (setf $) (value container key))

;;; Implementation for arrays

(defmethod $ ((container array) (key integer))
  (aref container key))

(defmethod (setf $) (value (container array) (key integer))
  (setf (aref container key) value))
==

And usage:

==
CL-USER(3): (defparameter a (vector 1 2 3 4 5))
A
CL-USER(4): ($ a 0)
1
CL-USER(5): (setf ($ a 0) 9)
9
CL-USER(6): a
#(9 2 3 4 5)
==

The nice thing is, that you *can* dispatch on the container,
the key and the value.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: merits of Lisp vs Python

2006-12-13 Thread hit_the_lights
Paul Rubin schrieb:

> Neil Cerutti <[EMAIL PROTECTED]> writes:
> > Is the above 'duck-typing' idiom considered very useful to a
> > Lisper? It seems logical to me that duck-typing works best in an
> > environment where it is ubiquitous. If users have to implement
> > accessors specifically to use your library, it is not as good as
> > if they had already implemented one as a matter of routine.

No, it's not as ubiquitious as in Python. I guess that has many
different reasons, probably many historical. IIRC Peter Seibel
writes in Practical Common Lisp that CLOS (and thus "defgeneric")
was introduced late in the CL standardization process. That my be
one reason why it is not used extensively by the rest of the
standardized language.

> It's a little more complicated than that, the classes involved have to
> have special interfaces to tell setf/getf what to do, sort of a
> compile time equivalent of __setattr__/__getattr__ if I remember right.

The code I've posted is all you need. You can specialize it for any
class (including numbers, functions, symbols etc.) you like.

In case of the lookup "($ a 0)" there is nothing done at compile
time, it's just a call of the generic function "$".

"(setf ($ a 0) 1)" is translated at compile time (setf is a macro)
to something like "(funcall #'(setf $) 1 a 0)".

-- 
http://mail.python.org/mailman/listinfo/python-list